Zend framework 1.8 recommended way to register a p

2019-02-27 11:58发布

问题:

In Zend Framework 1.8, what is the recommended way to register a new plugin in Zend Framework 1.8?

<?php
/**
 * Enter description here...
 *
 */
class Wenbert_Controller_Plugin_CheckHasAccess extends Zend_Controller_Plugin_Abstract {

    /**
     * Enter description here...
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        echo 'This plugin is called...';
    }
}

I remember doing something like this in versions prior to 1.8:

require_once 'Wenbert/Controller/Plugin/CheckHasAccess.php';
$frontController = Zend_Controller_Front::getInstance();
$frontController->registerPlugin(new Wenbert_Controller_Plugin_CheckHasAccess());

What would be the best / recommended way to do it in ZF 1.8?

Thanks in advance!

-Wenbert

回答1:

I'm assuming you mean the Zend_Application way of doing things. There's absolutely nothing wrong with the way you mentioned above. That said, you can specify which plugins to load in your application.ini, like so:

resources.frontController.plugins.foo = "My_Plugin_Foo"
resources.frontController.plugins.bar = "My_Plugin_Bar"

The only draw back that I am aware of with this approach, is that you cannot control the priority (location in the plugin stack) of the plugin... but for most (almost all cases) that shouldn't matter.