我在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非常相似,因此如果您希望传递额外的参数,请将其视为键/值数组。