我试着从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。你不能怀疑阿兰风暴的解决方案!看看那里!:) –