Reputation: 715
We have a plugin which has a postDispatch hook used to record usage statistics on a CMS developed using ZF1. After one of the records is generated, we need to get it's unique id to the view to be able to update it via AJAX requests.
Several posts offer different methods for setting view variables from the postDispatch function but none of them have worked for us. They all work on the preDispatch dough.
Method 1 from this answer works on preDispatch but not on postDispatch.
$view = Zend_Controller_Front::getInstance()
->getParam('bootstrap')
->getResource('view');
$view->recordUID = XXXXXX
Same for method 2
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
$viewRenderer->initView();
}
$view = $viewRenderer->view;
$view->recordUID = XXXXXX
Another user suggested elsewhere (can't find the reference now) to use Zend's registry which also worked on preDispatch but not on postDispatch
Zend_Registry::set('recordUID', 'XXXXXX');
It would seem the view object is lost after the request has been dispatched.
Additional information
Upvotes: 0
Views: 394
Reputation: 11242
The view has already been rendered when postDispatch() method of the plugin is called.
In your Controller action, do not use
$this->render('script');
but rather
$this->_helper->viewRenderer('script');
if needed.
There is an answer here link that offers a work-around, by rendering the view inside the plugin postDispatch():
class App_Plugin_MyPlugin extends Zend_Controller_Plugin_Abstract{
public function preDispatch (Zend_Controller_Request_Abstract $request){
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setNeverRender(true);
}
public function postDispatch(Zend_Controller_Request_Abstract $request){
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$view = $viewRenderer->view;
$view->variable = 'new value';
$viewRenderer->render();
}
}
Alternatively, you can set the variable inside the postDispatch() of the controller(you could also create a base controller that extends Zend_Controller_Action, override preDispatch and postDispatch there and let all controllers extend the base controller).
Upvotes: 1