我試着從this網站相同的代碼。Magento調度自定義事件不起作用
它工作正常,但觀察員的代碼似乎沒有工作。
我的意思是在觀察者方法中,我回顯了一些文字並且也使用了exit()
。但控制不會去那裏。我試圖調試很多,但無法獲得解決方案。
在此先感謝。
這是我的模塊的目錄結構的屏幕截圖。
app/etc/MyCompanyName_HelloWorld.xml
<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</MyCompanyName_HelloWorld>
</modules>
</config>
現在我的模塊文件
3210
<?xml version="1.0"?>
<config>
<modules>
<mycompanyname_helloworld>
<version>
0.1.0
</version>
</mycompanyname_helloworld>
</modules>
<frontend>
<routers>
<!-- the <helloworld> tagname appears to be arbitrary, but by
convention is should match the frontName tag below-->
<helloworld>
<use>standard</use>
<args>
<module>MyCompanyName_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
<!--Custom events-->
<global>
<events>
<my_custom_event>
<observers>
<mycompanyname_helloworld_my_custom_event_observer>
<type>singleton</type>
<class>helloworld/observer</class>
<method>my_custom_method</method>
</mycompanyname_helloworld_my_custom_event_observer>
</observers>
</my_custom_event>
</events>
</global>
<!--//Custom events-->
</config>
Observer.php
<?php
/**
* Created by PhpStorm.
* User: pratik
* Date: 9/4/15
* Time: 7:45 AM
*/
class MyCompanyName_HelloWorld_Model_Observer{
public function my_custom_method($observer){
$eventName = $observer->getEvent();
echo "Hi i am inside event".$eventName; exit;
}
}
IndexController.php
<?php
/**
* Created by PhpStorm.
* User: pratik
* Date: 9/4/15
* Time: 7:32 AM
*/
class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
echo "In index controller";
//Now dispatching event(Sending off the event)
$arrToObservers = array('cid'=>'123');
Mage::dispatchEvent('my_custom_event',$arrToObservers);
////Now dispatching event(Sending off the event)
echo "after dispatch";
}
}
和輸出我得到(無Magento的執行我的觀察員echo語句)
In index controller --after dispatch
但它應該打印過寫在觀察者Hi i am inside event
文本。
你的模塊名稱應區分大小寫,即'MyCompanyName_HelloWorld',而不是在你的config.xml –
也全部小寫的應用程序的/ etc/XML文件應該是'應用/ etc/modules/MyCompanyName_HelloWorld.xml' –
Robbie先生:)我跟隨了一些來自hello world模塊的部分。 http://stackoverflow.com/questions/576908/how-to-create-a-simple-hello-world-module-in-magento。你不能懷疑阿蘭風暴的解決方案!看看那裏!:) –