在视图渲染之前执行函数
我是 Zend Framework MVC 的新手。我喜欢使用 MVC 环境的很多事情,但有时发现自己对它的结构感到困惑。
我有一个简单的任务,我想标记我们网站上的某些用户以跟踪他们的活动。为此,我在数据库中设置了一个简单的表,并开始将我的 _initTracking() 函数编码到引导程序中。然后我意识到我从错误的方向接近这个 - 我希望这是最后触发的函数之一,以避免用标头重定向搞乱我的跟踪条目,并确保所有自动加载的类都存在。我该怎么做?是否有“onBeforeRender”类型的函数?如果有我也找不到。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用 ZF 插件。您可以在插件的
postDispatch()
或dispatchLoopShutdown()
方法中跟踪用户的操作,具体取决于您的跟踪需要的粒度。一些有关 ZF 插件的阅读 - http://framework.zend.com/ Manual/en/zend.controller.plugins.html
还有一篇关于 Zend Framework 中请求生命周期的非常简洁的文章 - http://www.eschrade.com/page/zend-framework-request-lifecycle-4b9a4288。
I would suggest using a ZF plugin. You could track user's actions in plugin's
postDispatch()
ordispatchLoopShutdown()
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.
我认为最好的位置是控制器中的 postDispatch() 挂钩。
看看 http://framework.zend.com/manual/ en/zend.controller.action.html,特别是关于预调度和后调度挂钩的部分。
这适合将您的跟踪代码放置在基本控制器中 - 您的操作控制器将扩展该控制器,将跟踪代码保留在一个位置。
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.