我如何在实例化期间传递参数给我的Zend框架插件


How do I pass parameters to my Zend Framework plugins during instantiation?

我在libraries路径上定义了一个插件,使用正确的目录结构,并在application.ini文件中知道它的存在。插件加载,我的preDispatch()方法触发。但是,我如何在实例化期间传递参数给插件?

下面是我的代码:
class Project_Controller_Plugin_InitDB extends Zend_Controller_Plugin_Abstract {
    private $_config = null;
    public function __contruct($config){
        $this->_config = $config;
    }
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $db = Zend_DB::factory("Pdo_Mysql", $this->_config);
        Zend_Registry::set("db", $db);
    }
}

具体来说,我如何将$config传递给__construct()方法?

谢谢,<<p> 解决方案/strong>

这是我最后的结果(感谢Phil Brown!):

在我的application.ini文件:

autoloaderNamespaces[] = "MyProjectName_"

在我的Bootstrap.php文件:

protected function _initFrontControllerPlugins() {
    $this->bootstrap('frontcontroller');
    $frontController = $this->getResource('frontcontroller');
    $plugin = new MyProjectName_Controller_Plugin_InitDB($this->_config->resources->db->params);
    $frontController->registerPlugin($plugin);
}

只需在Bootstrap类中手动注册您的插件

protected function _initFrontControllerPlugins()
{
    $this->bootstrap('frontcontroller');
    $frontController = $this->getResource('frontcontroller');
    // Config could come from app config file
    // or anywhere really
    $config = $this->getOption('initDb');
    $plugin = new Project_Controller_Plugin_InitDB($config);
    $frontController->registerPlugin($plugin);
}

使用Zend Registry

你看了吗?$Config结构与Zend_Config非常相似,因此如果您希望传递额外的参数,请将其视为键/值数组。