2011-12-18 91 views
-2

什麼是最好的方式來啓動插件僅適用於控制器中的一個動作。Zend Framework - 插件

補充:

這裏是一小段代碼,我想在的IndexController

class MyApp_Plugin_MyPlugin extends Zend_Controller_Plugin_Abstract 
{ 
    public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
     print 'hello world'; 
    } 
} 

我希望你阻特

這段代碼將有助於運行該插件testAction

最好的問候

+1

某些代碼可能對您有所幫助,並描述您正在嘗試完成的任務。 – Zoot 2011-12-18 23:43:33

回答

2

你可以直接調用插件

public function someAction() 
{ 
    // The action in the controller you want the plugin to run 

    $p = new Application_Plugin_Something(); 
    $p->doSomething(); 
} 

或引導其註冊,並有插件檢查,看看是執行它

class Application_Plugin_Example extends Zend_Controller_Plugin_Abstract 
{ 
    public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
     $module  = $request->getModuleName(); 
     $controller = $request->getControllerName(); 
     $action  = $request->getActionName(); 

     if ($module != 'default' && $controller != 'TheController' && $action != 'TheAction') { 
      return; 
     } 

     // plugin code here... 
    } 
} 

// in bootstrap 

Zend_Controller_Front::getInstance() 
         ->registerPlugin(
          new Application_Plugin_Example() 
         ); 

在插件代碼,只需更換「默認」用正確的模塊,「TheController」和「TheAction」用你希望插件運行的控制器和動作。