我在Magento有一个控制器如下:
- #File: ./app/local/FilFact/Test/IndexController
- class FilFact_Test_IndexController extends Mage_Core_Controller_Front_Action{
- public function indexAction(){
- $this->_testConfig();
- }
- }
我需要添加两个envent:
在索引操作之前
索引动作后
我该怎么办?
这很简单,因为Mage_Core_Controller_Varien_Action基类提供了前/后调度事件.
如果您打开了Mage_Core_Controller_Varien_Action类,您可以找到两种方法:preDispatch()和postDispatch()
这些方法执行一些任务,最重要的是消除三个事件.
- controller_action_(pre|post)dispatch
- controller_action_(pre|post)dispatch_{{routeName}}
- controller_action_(pre|post)dispatch_{{fullActionName}}
fullActionName是路由名称,控制器名称和由“_”和所有小写分隔的操作名称. (见Mage_Core_Controller_Varien_Action::getFullActionName参考)
/app/code/local/FilFact/Test/etc/config.xml
- <?xml version="1.0"?>
- <config>
- <modules>
- <FilFact_Test>
- <version>1.0.0</version>
- <FilFact_Test>
- </modules>
- <global>
- <models>
- <FilFact_Test>
- <class>FilFact_Test_Model</class>
- </FilFact_Test>
- </models>
- </global>
- <frontend>
- <routers>
- <filfact>
- <use>standard</use>
- <args>
- <module>FilFact_Test</module>
- <frontName>filfact</frontName>
- </args>
- </filfact>
- </routers>
- <events>
- <controller_action_predispatch_filfact_index_index>
- <observers>
- <FilFact_Test>
- <class>FilFact_Test/Observer</class>
- <method>indexPreDispatch</method>
- </FilFact_Test>
- </observers>
- </controller_action_predispatch_filfact_index_index>
- <controller_action_postdispatch_filfact_index_index>
- <observers>
- <FilFact_Test>
- <class>FilFact_Test/Observer</class>
- <method>indexPostDispatch</method>
- </FilFact_Test>
- </observers>
- </controller_action_postdispatch_filfact_index_index>
- </events>
- </frontend>
- </config>
/app/code/local/FilFact/Test/Model/Observer.PHP
- <?PHP
- class FilFact_Test_Model_Observer
- {
- public function indexPreDispatch(Varien_Event_Observer $observer)
- {
- // TODO: Your code
- }
- public function indexPostDispatch(Varien_Event_Observer $observer)
- {
- // TODO: Your code
- }
- }