Skip to content

使用Library方式將Zend Framework整合到CodeIgniter

  網路上教大家結合Zend Framework(ZF)跟CodeIgniter(CI)的方式大多是使用hook方式。但存在著潛在的危險性,因為hook是寫在pre_controller(在所有的控制器(controller)呼叫之前執行,此時所有的基礎類別、路由、安全檢查都已經完成。),這樣子的話假如ZF某些功能影響到CI,就會導致CI整個無法運作,且很難做Debug。

  接下來教大家如果利用Library的方式將ZF整合到CI。

1.從Zend Framework官方網站下載ZF。

2.解壓縮ZF,複製Library下面的Zend目錄到CI 的application/libraries/下

3.在application/libraries下新增一個zend.php,程式碼如下


if (!defined('BASEPATH')) {exit('No direct script access allowed');}

/**
 * Zend Framework Loader
 *
 * Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library')
 * in CI installation's 'application/libraries' folder
 * You can put it elsewhere but remember to alter the script accordingly
 *
 * Usage:
 *   1) $this->load->library('zend', 'Zend/Package/Name');
 *   or
 *   2) $this->load->library('zend');
 *      then $this->zend->load('Zend/Package/Name');
 *
 * * the second usage is useful for autoloading the Zend Framework library
 * * Zend/Package/Name does not need the '.php' at the end
 */
class CI_Zend
{
 /**
  * Constructor
  *
  * @param string $class class name
  */
 function __construct($class = NULL)
 {
  // include path for Zend Framework
  // alter it accordingly if you have put the 'Zend' folder elsewhere
  ini_set('include_path',
  ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');

  if ($class)
  {
   require_once (string) $class . EXT;
   log_message('debug', "Zend Class $class Loaded");
  }
  else
  {
   log_message('debug', "Zend Class Initialized");
  }
 }

 /**
  * Zend Class Loader
  *
  * @param string $class class name
  */
 function load($class)
 {
  require_once (string) $class . EXT;
  log_message('debug', "Zend Class $class Loaded");
 }
}

4.接下來就可以使用ZF


class Welcome extends Controller {

 function Welcome()
 {
  parent::Controller();
 }

 function index()
 {
  $this->load->library('zend', 'Zend/Service/Flickr');
  // newer versions of CodeIgniter have updated its loader API slightly,
  // we can no longer pass parameters to our library constructors
  // therefore, we should load the library like this:
  // $this->load->library('zend');
  // $this->zend->load('Zend/Service/Flickr');

  $flickr = new Zend_Service_Flickr('YOUR_FLICKR_API_KEY');

  $results = $flickr->tagSearch('php');
  foreach ($results as $result)
  {
   echo $result->title . '';
  }
  //$this->load->view('welcome_message');
 }
}

延伸閱讀:Using Zend Framework with CodeIgniter

開心農場的背後技術分享

  最近一直很熱門的Facebook小遊戲「開心農場」,變成了大家茶餘飯後的話題。而這個開發成本不到十萬的Social Game,迅速讓全台灣許多網友成了農夫…..想必這種令人咋舌的應用程式,大家很希望瞭解背後的技術
  其實開心農場約在去年9月就在xiaonei(校內網)上線了,後來陸續在51ManyouQQ等中國各大SNS平台,每日的登陸使用者超過1500萬。
  簡報是Five Minutes公司程延輝(康天)介紹開心農場架構,對於開發者來說,具有相當實用參考價值的一份文件。
簡報中提供主要架構的困難有:
1. 如何儲存大量的使用者資料
2. 如何應對大量的Request
3. 如何改善頻繁的資料修改
   而在文件中也提出相關的最佳化解決方式。大家有空可以參考

Get Adobe Flash playerPlugin by wpburn.com wordpress themes