Zend Framework: how to enter a controller property from Zend_Controller_Plugin

I wrote a plugin that should set the property on the controller that is currently being sent. For example, if my plugin is:

class Application_Plugin_Foo extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        // Get an instance of the current controller and inject the $foo property
        // ???->foo = 'foo';
    }
}

I want to be able to do this:

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
        {
            $this->view->foo = $this->foo;
        }
    }
}

Any help is much appreciated!

+3
source share
2 answers

The action controller is not directly accessible directly from the front controller plugin. This is the dispatcher that instantiates the controller object, and it does not seem to save it anywhere.

However, the controller is accessible from any registered action assistants. Since action assistants have a hook preDispatch, you can do your injection there.

, library/My/Controller/Helper/Inject.php:

class My_Controller_Helper_Inject extends Zend_Controller_Action_Helper_Abstract
{
    public function preDispatch()
    {
        $controller = $this->getActionController();
        $controller->myParamName = 'My param value';
    }
}

application/Bootstrap.php:

protected function _initControllerInject()
{
    Zend_Controller_Action_HelperBroker::addHelper(
        new My_Controller_Helper_Inject()
    );
}

, , My_ configs/application.ini:

autoloaderNamespaces[] = "My_"

:

public function myAction()
{
    var_dump($this->myParamName);
}

: preDispatch(), , , forward().

+3

API, ( , , ). , , , .

class Application_Plugin_Foo extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $yourParam = 'your value';
        if($request->getParam('yourParam')) {
           // decide if you want to overwrite it, the following assumes that you do not care
           $request->setParam('yourParam', $yourParam);
        }
    }
}

Zend_Controller_Action::xxxAction(): $this->getParam('yourParam');


Zend_Controller_Action_Helper_Abstract

MWOP, : ZF Action Controllers. Zend_Controller_Action $this->yourParam.

+1

Source: https://habr.com/ru/post/1767561/


All Articles