我在libraries
路徑上定義了一個插件,使用正確的目錄結構,並使其在application.ini
文件中已知存在。該插件加載,並且我的preDispatch()
方法觸發。但是,如何在實例化過程中將參數傳遞給插件?如何在實例化過程中將參數傳遞給我的Zend Framework插件?
這裏是我的代碼:
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()
方法?
感謝,
解決方案
這裏是我結束了(感謝菲爾·布朗!):
在我application.ini
文件:
autoloaderNamespaces[] = "MyProjectName_"
在我Bootstrap.php
file:
protected function _initFrontControllerPlugins() {
$this->bootstrap('frontcontroller');
$frontController = $this->getResource('frontcontroller');
$plugin = new MyProjectName_Controller_Plugin_InitDB($this->_config->resources->db->params);
$frontController->registerPlugin($plugin);
}
Right,AFAIK,如果你的插件需要參數,你不能通過'application.ini'實例化它。您需要在引導期間手動實例化,並在那裏傳遞參數,如上所述。 – 2011-04-11 01:13:43
@David雖然我更喜歡我的答案中的方法,但您可以隨時將「bootstrap」invokeArg從插件中的前端控制器實例中提取出來,以便您訪問配置和資源。這依賴於FC的單身性質,儘管我總是嘗試和勸阻 – Phil 2011-04-11 01:17:37
非常好!謝謝! – 2011-04-11 20:27:51