In my first post below, I covered how to create a FC plugin to ensure that a user is authenticated when they access every controller.
As part of our layout, we want to include a generic style sheet in each page, and a custom style sheet for each controller to each rendered page. As in the previous post, we wanted effectively to run some code in each and every controller, but did not want to have to include the code in each and every controller. As we already had a FC plugin that was handling the authentication, I decided to modify this to set a view variable that can be accessed from the layout.
Basically, this process takes 3 easy steps…
- Get an instance of the ViewRenderer
- Initialise the view
- Set the view variable
This is pretty straight forward when you understand what you want to achieve, but took some getting right for me, thanks again to everyone in the #zftalk IRC channel for their patience on this one!
// set a view variable with the controller name for access in the layout // not strictly to do with authentication but it can sit here as a bonus // create an instance of the view renderer $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); // initilaise the view $viewRenderer->initView(); // set a view variable to be the controller name $viewRenderer->view->controller = $request->getControllerName();
Basically that covers it, the trickiest part for me was learning to use the Zend_Controller_Action_HelperBroker to get the helper I needed to set the view variables. I hope this helps someone.