Setting view variables from within a FC Plugin

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…

  1. Get an instance of the ViewRenderer
  2. Initialise the view
  3. 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
// 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.

More reading…

Your Comments are Awesome, Please Leave Them!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s