Magento 路由器:如何捕获所有 URL 中的参数?
考虑一个小型且基本的联盟系统。 我想要一个像
www.myshop.com/mynewproduct.html?afid=123
每次在 URL 中找到 afid 时,都应该调用一个方法(基本上是在会话中保存“afid”,当客户购买东西时,我想跟踪它)。
Think of a small and basic affiliate system. I want an URL like
www.myshop.com/mynewproduct.html?afid=123
Every time afid
is found in the URL, a method should be called (basically to save "afid" in the session and when the customer buys stuff, I want to track it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您不需要路由器。 您需要设置一个在每次页面加载时触发的事件侦听器,然后访问请求集合中的变量。 该controller_front_init_routers事件应该做。
因此,使用以下内容设置模块的配置
,然后创建以下类
interceptMethod
可以命名为您想要的任何名称。You don't need a router for this. You'll want to setup an event listener that fires for every page load, and then access the variables in the request collection. The controller_front_init_routers event should do.
So, setup your module's config with the following
And then create the following class
The
interceptMethod
can be named whatever you want.我知道这是一个非常古老的答案,但值得一提的是,如果我们打算将这些参数存储在会话中,那么我们不应该使用
controller_front_init_routers
事件,这是原始问题的场景。 例如,如果您此时实例化customer/session
,您将无法再执行客户登录。 Alan 在 http://alanstorm.com/magento_sessions_early 中亲自指出了这一点。 顺便说一句,感谢艾伦这篇精彩的文章。I know this is a very old answer, but it is valid to mention we shouldn't use the
controller_front_init_routers
event if we intend to store those parameters in session, which is the scenario for the original question. For example, if you instantiatecustomer/session
at this point you won't be able to perform a customer login anymore. Alan pointed this himself in http://alanstorm.com/magento_sessions_early. BTW, thanks Alan for this great article.