2014-10-03 45 views
0

我想创建controller_action_predispatch事件:Magento的:controller_action_predispatch不工作

这是我兆字节/ Pushnotification的/ etc/config.xml文件中的代码

<events> 
      <controller_action_predispatch_mbyte_pushnotification_index_index> 
       <observers> 
        <Mbyte_Pushnotification> 

         <class>Mbyte_Pushnotification/Observer</class> 
         <method>indexPreDispatch</method> 
        </Mbyte_Pushnotification> 
       </observers> 
      </controller_action_predispatch_mbyte_pushnotification_index_index> 
    </events> 

我的控制器代码:字节/ Pushnotification /控制器/ IndexController.php文件代码

class Mbyte_Pushnotification_IndexController extends Mage_Core_Controller_Front_Action 
{ 

    public function indexAction() 
    { 
    Mage::log('Run',null,'log.log'); 

    } 
    } 

观察文件的代码:

class Mbyte_Pushnotification_Model_Observer { 
       public function indexPreDispatch($observer) 
       {  
       Mage::log('Check',null,'observer.log'); 

         }} 

controller_action_predispatch不起作用。 有什么我做错了吗?

+0

它不会纠正你的错误,但投出您$观察者像'公共职能indexPreDispatch(Varien_Event_Observer $观察员)' – apouey 2014-10-03 08:08:07

+0

我试过这个,但没有为我工作 – 2014-10-03 09:15:09

回答

0

事件触发应该是

controller_action_predispatch_pushnotification_index_index 代替 controller_action_predispatch_mbyte_pushnotification_index_index

的情况下,你没有修改控制器的前端名称( “pushnotification”)。

因此,你需要更新你的config.xml中

<events> 
     <controller_action_predispatch_pushnotification_index_index> 
      <observers> 
       <Mbyte_Pushnotification> 
        <class>Mbyte_Pushnotification_Model_Observer</class> 
        <method>indexPreDispatch</method> 
       </Mbyte_Pushnotification> 
      </observers> 
     </controller_action_predispatch_pushnotification_index_index> 
</events> 

你也可以写

<events> 
     <controller_action_predispatch_pushnotification_index_index> 
      <observers> 
       <Mbyte_Pushnotification> 
        <class>pushnotification/observer</class> 
        <method>indexPreDispatch</method> 
       </Mbyte_Pushnotification> 
      </observers> 
     </controller_action_predispatch_pushnotification_index_index> 
</events> 

,如果你命名了模块 “pushnotification”,我以为。

0

据Magento的定义事件取决于应该

controller_action_predispatch_frontName_Controller_Youaction 

它定义如下:

Mage::dispatchEvent('controller_action_predispatch_' . $this->getFullActionName(), 
     array('controller_action' => $this)); 
+0

我试过这个,但是这对我没有用。你能用例子来解释吗? – 2014-10-03 09:51:47