2011-12-18 94 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”用你希望插件运行的控制器和动作。