简体   繁体   中英

Executing a function before view renders

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

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM