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.

Community
  • 1
  • 1
Sonny
  • 8,204
  • 7
  • 63
  • 134
  • you do know you can modfiy head* in your controller action ? e.g. $this->view->headScript()->appendFile('/js/whatever.js'); – Rufinus Feb 13 '10 at 16:11
  • 1
    Yes, I do, and I use that feature. I've updated my post again to be more specific. – Sonny Feb 14 '10 at 12:45
  • ok now i know what you are tring to do. my best suggest is to extend the view helpers. the involved view helper using the placeholder-container view helper. by extending this classes it should be possible to do what you want. (but, to be honest, i see no big benefit in it, it would be better if you use a small weightless http daemon like lighttpd to only serve this static files. – Rufinus Feb 15 '10 at 23:12

1 Answers1

13

ZF1 Dispatch Process Overview (c) by Thorsten Ruf

see Orginal PDF created by Thorsten Ruf (Mirror)

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

EDIT: For ZendFramework2 see http://zendframework2.de/en/cheat-sheet.html or this gdoc

petres
  • 582
  • 2
  • 19
Rufinus
  • 29,200
  • 6
  • 68
  • 84
  • 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 Feb 12 '10 at 17:32
  • 1
    Oooh, nice. I wish they'd made it just a little bigger though. – Robin M. Canaday Feb 12 '10 at 18:44
  • 1
    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 M. Canaday Feb 12 '10 at 18:55
  • @Robin, thanks for the pointer, I will look into how that is done. – Sonny Feb 14 '10 at 12:52
  • I am marking your answer as the solution, Rufinus. I will definitely be using this knowledge later on. – Sonny Feb 15 '10 at 23:30
  • Your link no longer works. Could you update it to an archive.org link or to wherever it's moved? – GordonM Mar 17 '15 at 10:27
  • thanks i removed the image, the pdf link still works. – Rufinus Mar 17 '15 at 12:37