在 Magento 中为控制器的动作之前/之后添加事件 [英] Add events for before/after a action of a controller in Magento

查看:29
本文介绍了在 Magento 中为控制器的动作之前/之后添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 索引动作
after 索引动作

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 类,你会发现两种方法:preDispatch()postDispatch()

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 中为控制器的动作之前/之后添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆