Start
Plugin 需要繼承 Zend_Controller_Plugin_Abstract
,跟一般所知的 plugin 一樣,都是用觀察者模式實作。官方有提供 Example 如下:
class Application_Plugin_Test extends Zend_Controller_Plugin_Abstract {
public function routeStartup(Zend_Controller_Request_Abstract $request) { $this->getResponse() ->appendBody("<p>routeStartup() called</p>\n"); }
public function routeShutdown(Zend_Controller_Request_Abstract $request) { $this->getResponse() ->appendBody("<p>routeShutdown() called</p>\n"); }
public function dispatchLoopStartup( Zend_Controller_Request_Abstract $request) { $this->getResponse() ->appendBody("<p>dispatchLoopStartup() called</p>\n"); }
public function preDispatch(Zend_Controller_Request_Abstract $request) { $this->getResponse() ->appendBody("<p>preDispatch() called</p>\n"); }
public function postDispatch(Zend_Controller_Request_Abstract $request) { $this->getResponse() ->appendBody("<p>postDispatch() called</p>\n"); }
public function dispatchLoopShutdown() { $this->getResponse() ->appendBody("<p>dispatchLoopShutdown() called</p>\n"); } }
|
Zend Framework 有預設 plugin 放的位置,可參考 Resource Autoloader
再來就是要註冊 plugin,用程式碼註冊的方法:
$front = Zend_Controller_Front::getInstance(); $front->registerPlugin(new Application_Plugin_Test());
|
用 application.ini
註冊的方法:
resources.frontController.plugins[] = Application_Plugin_Test
|
Reference