Zend Framework - Getting access to controller and action names in resource class (inherits after Zend_Controller_Resource_ResourceAbstract)
-
26-06-2021 - |
Question
I need to create my own resource which sends some information to Zend_View
instance which depends on currently working controller and action.
I've got this code:
$view = $bootstrap->getResource('layout')->getView();
$front = $bootstrap->getResource('frontController');
$front->setRequest(new Zend_Controller_Request_Http);
$controller = $front->getRequest()->getControllerName();
$action = $front->getRequest()->getActionName();
$view->headTitle(
$this->getPage()
->setController($controller)
->setAction($action)
->getTitle()
);
but $controller
and $action
are empty. I don't know if I'm doing something wrong or getting access to controller and action names is impossible in resource.
Solution
You can't access the request object in a resource because it doesn't exist yet. The request object gets set during dispatch which happens after the application has been bootstrapped. It sounds like this logic should be moved to a controller plugin instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow