title: Zend Framework - Last Code to Execute Before Layout is Rendered
body: <p>I want to execute some code right before the layout is rendered, after all other code is executed.  Where would I put that code?</p>
  
  <p>I am specifically trying to modify the files referenced in the headLink, headScript, and inlineScript view helpers before they're used by the layout.</p>
  
  <p>Here are the steps I want to take:</p>
  
  <ol>
  <li>Loop over the files in those view helpers</li>
  <li>Make a list of the local files</li>
  <li>Remove local files from the view helpers</li>
  <li>Reference the local file list as a parameter to a server script that combines them for a single HTTP request</li>
  <li>Add that new combine script reference to the appropriate view helper</li>
  </ol>
  
  <hr>
  
  <p>It doesn't appear that a Front Controller Plugin is going to help me accomplish this, and here's why:</p>
  
  <ol>
  <li><code>postDispatch()</code> gets executed after every controller action that's executed, and I need the full list of stylesheets/javascripts</li>
  <li><code>dispatchLoopShutdown()</code> gets executed after the controller action loop, but the layout has already been rendered at this point</li>
  </ol>
  
  <hr>
  
  <p>As Rufinus suggested, I solved this by extending the View Helpers.  <a href="https://stackoverflow.com/questions/2269306/modify-stack-in-the-zend-headscript-view-helper">My question from that angle as well as the solution is here</a>.</p>