2011-09-22 11 views
12

のコントローラのアクションの前/後のイベントを追加します。 afterindex action私は以下のようにMagentoのコントローラを持っているMagentoの

どうすればいいですか?

+0

2つのイベントを提供して、他のモジュールがそれらに接続できるようにしたいと思いますか? – Simon

+0

はい、私は次のような使い方をしています:次の文を追加してください: 'Mage :: dispatchEvent( 'test_index_index_before_action'、$ data);しかしそれは良くありません。以下の答えはより良いです。 – vietean

答えて

36

これは、Mage_Core_Controller_Varien_Action基本クラスがプレディスパッチイベントとポストディスパッチイベントを提供するので簡単です。

あなたはMage_Core_Controller_Varien_Actionクラスを開く場合は、二つの方法を見つける:preDispatch()postDispatch()

これらの方法は、いくつかのタスクを実行し、最も重要な3つのイベントをオフに解雇します。

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 
    } 
} 
+1

私は試みます。 :)あなたの答えを愛する。 – vietean

+0

素晴らしい私は私のために働く。しかし、私はそれがどのように機能するのか分かりません。 ^^ – vietean

+3

私はインクルードと説明の答えを更新します。 –

関連する問題