views:

347

answers:

1

I want to execute some code right before the layout is rendered, after all other code is executed. Where would I put that code?

I am specifically trying to modify the files referenced in the headLink, headScript, and inlineScript view helpers before they're used by the layout.

Here are the steps I want to take:

  1. Loop over the files in those view helpers
  2. Make a list of the local files
  3. Remove local files from the view helpers
  4. Reference the local file list as a parameter to a server script that combines them for a single HTTP request
  5. Add that new combine script reference to the appropriate view helper

It doesn't appear that a Front Controller Plugin is going to help me accomplish this, and here's why:

  1. postDispatch() gets executed after every controller action that's executed, and I need the full list of stylesheets/javascripts
  2. dispatchLoopShutdown() gets executed after the controller action loop, but the layout has already been rendered at this point

As Rufinus suggested, I solved this by extending the View Helpers. My question from that angle as well as the solution is here.

+10  A: 

see http://www.php-professionals.com/2009/01/12/really-helpful-zf-dispatch-flow-diagram/ (EDIT: Created by Thorsten Ruf: http://nethands.de/download/zenddispatch_en.pdf )

the very last part you can access via plugin should be dispatchLoopShutdown

Zend Framework Dispatch Diagramm

Rufinus
Very nice flow diagram! I am unclear on how to get code to execute at that stage though. I've updated my post to clarify what I am trying to accomplish.
Sonny
Oooh, nice. I wish they'd made it just a little bigger though.
Robin Canaday
Zend_Controller_Action::dispatch() calls the postDispatch() after it calls the action method, before it destroys the action controller instance. You can override postDispatch() in your action controller to make changes to view before rendering (it's provided for that purpose.) I think you have to use a front controller plugin to make things happen during dispatch loop shutdown...
Robin Canaday
@Robin, thanks for the pointer, I will look into how that is done.
Sonny
I am marking your answer as the solution, Rufinus. I will definitely be using this knowledge later on.
Sonny