views:

30

answers:

3

I'm new to Zend Framework MVC. I love a lot of things about working with an MVC environment, but find myself confused about it structurally sometimes.

I have a simple task, I'd like to flag certain users on our site to track their movements. For this I've set up a simple table in the database, and started to code in my _initTracking() function into the bootstrap. I then realized I was approaching this from the wrong direction - I'd like this to be one of the last functions that fires, to avoid mucking up my tracking entries with header redirects, and to ensure all autoloaded classes are present. How do I do this? Is there an "onBeforeRender" type of function? If there is I couldn't find it.

Thanks

A: 

Ended up putting this in the layout scripts. There's probably a better way of doing this, but in my case (having all views I wanted the code to run in under 2 layouts) it was the easiest, and accomplished my goal.

Mahdi.Montgomery
And I thought this would close the question but apparently not
Mahdi.Montgomery
You can accept your own answer. Or chose to close/delete it. But best just wait a while to see if someone posts a better answer and then accept that.
Iznogood
A: 

I think the best place for this would be in a postDispatch() hook in your controller.

Have a look at http://framework.zend.com/manual/en/zend.controller.action.html, specifically the section on Pre- and Post-Dispatch Hooks.

This would suit placing your tracking code in a base controller - which your action controllers would extend, keeping the tracking code in one location.

Alex Osborn
+1  A: 

I would suggest using a ZF plugin. You could track user's actions in plugin's postDispatch() or dispatchLoopShutdown() method, depending on how granular your tracking needs to be.

Some reading about ZF plugins - http://framework.zend.com/manual/en/zend.controller.plugins.html

Also a really neat article about request lifecycle in Zend Framework - http://www.eschrade.com/page/zend-framework-request-lifecycle-4b9a4288.

Vika
Perfect! Thanks so much. I couldn't find this, but it is exactly what I needed. Much cleaner too.
Mahdi.Montgomery