我已使用正确的目录结构在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()
方法?
谢谢,
解决方案
这是我最终得到的结果(感谢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);
}
答案 0 :(得分:4)
只需在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);
}
答案 1 :(得分:0)
答案 2 :(得分:-1)
您是否阅读过this? $ Config结构非常接近Zend_Config,因此如果您希望传递额外的参数,请将其视为键/值数组。