【发布时间】:2015-09-04 02:53:12
【问题描述】:
我从this 站点尝试了相同的代码。
它运行良好,但观察者的代码似乎不起作用。
我的意思是在观察者方法中我已经回显了一些文本并且也使用了exit()。但控制不去那里。我尝试了很多调试,但无法得到解决方案。
提前致谢。
这是我的模块目录结构的屏幕截图。
app/etc/MyCompanyName_HelloWorld.xml
<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</MyCompanyName_HelloWorld>
</modules>
</config>
现在是我的模块文件
config.xml
<?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 执行我的观察者回显语句)
In index controller --after dispatch
但它应该打印Hi i am inside event 文本也写在观察者中。
【问题讨论】:
-
您的模块名称应该区分大小写,即
MyCompanyName_HelloWorld,而不是config.xml中的全部小写 -
app/etc/ XML 文件也应该是
app/etc/modules/MyCompanyName_HelloWorld.xml -
Robbie sir :) 我从这里跟随了 hello world 模块的一些部分。 stackoverflow.com/questions/576908/… 。你不能怀疑艾伦风暴的解决方案!看看那里! :)
-
我想你会发现他的回答是错字而不是错误。他谈论该文件的第一个例子提到移动到
app,然后触摸etc/modules/YourModule_Example.xml
标签: php magento events design-patterns observers