问题描述
我在Magento中有一个控制器,如下所示:
I have a controller in Magento as below:
#File: ./app/local/FilFact/Test/IndexController
class FilFact_Test_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$this->_testConfig();
}
}
我需要为以下事件添加两个事件:
before
index action
after
index action
I need to add two events for:before
index action
after
index action
我该怎么办?
推荐答案
这很简单,因为Mage_Core_Controller_Varien_Action基类提供了事前/事后调度事件.
This is simple as the Mage_Core_Controller_Varien_Action base class provides pre/post dispatch events.
如果打开Mage_Core_Controller_Varien_Action类,则会发现两种方法:和
If you open up the Mage_Core_Controller_Varien_Action class you find two methods: preDispatch() and postDispatch()
这些方法执行一些任务,最重要的是触发三个事件.
These method perform a few tasks and most importantly fire off three events.
controller_action_(pre|post)dispatch
controller_action_(pre|post)dispatch_{{routeName}}
controller_action_(pre|post)dispatch_{{fullActionName}}
fullActionName是路由名称,控制器名称和操作名称,用"_"和所有小写字母分隔. (请参见 Mage_Core_Controller_Varien_Action: :getFullActionName 供参考)
The fullActionName is the route name, the controller name, and the action name separated by '_' and all lower case. (See Mage_Core_Controller_Varien_Action::getFullActionName for reference)
/app/code/local/FilFact/Test/etc/config.xml
/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
/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
}
}
这篇关于在Magento中的控制器动作之前/之后添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!