毕竟用惯了ZF,所以现在用CI有时候也想从ZF里面扒点东西,
提醒,只能用PHP5。不兼容PHP4 ,因为ZF是纯粹的PHP5~~
網路上教大家結合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');}
PHP复制代码 /**
* 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
PHP复制代码 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 |