开发者

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

开发者 https://www.devze.com 2023-02-24 05:01 出处:网络
I have defined a plugin on the libraries path, using the correct directory structure, and have made it\'s presence known in the application.ini file. The plugin loads, and my preDispatch() method fire

I have defined a plugin on the libraries path, using the correct directory structure, and have made it's presence known in the application.ini file. The plugin loads, and my preDispatch() method fires. But, how can I pass parameters to the plugin during instantiation?

Here is my code:

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);
    }

}

Specifically, how do I pass $confi开发者_Go百科g to the __construct() method?

Thanks,

Solution

Here is what I ended up with (Thanks to Phil Brown!):

In my application.ini file:

autoloaderNamespaces[] = "MyProjectName_"

In my 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);
}


Simply register your plugin manually in your Bootstrap class

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);
}


Use Zend Registry


Have you read this? $Config structure resembles Zend_Config very closely, so if you wish to pass extra parameters, treat it as an array of key/values.

0

精彩评论

暂无评论...
验证码 换一张
取 消