在视图渲染之前执行函数

发布于 2024-09-19 12:28:19 字数 282 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-09-26 12:28:19

我建议使用 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() 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.

反话 2024-09-26 12:28:19

最终将其放入布局脚本中。可能有更好的方法来做到这一点,但就我而言(我希望代码在两种布局下运行的所有视图)这是最简单的,并且实现了我的目标。

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.

唔猫 2024-09-26 12:28:19

我认为最好的位置是控制器中的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文