Magento发送自定义事件不起作用

时间:2015-09-04 02:53:12

标签: php magento events design-patterns observers

我在this网站上尝试了相同的代码。

效果很好,但观察者的代码似乎没有用。

我的意思是在观察者方法中我回复了一些文本并使用了$hasilmsisdn=explode(";", $msisdn); $hasilaccid=explode(";", $accid); foreach ($hasilmsisdn as $value "&&" $hasilaccid as $value2) { $sql = "INSERT INTO complaint_detail (MSISDN,ACCID) VALUES ('$value','$value2');"; $run=mysql_query($sql); } 。但控制不会去那里。我试图调试很多,但无法得到解决方案。

提前致谢。

这是我模块的目录结构的屏幕截图。

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

我得到的输出(没有magento执行我的观察者echo语句)

<?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"; } }

但它应该打印In index controller --after dispatch文本,也是用观察者写的。

enter image description here

1 个答案:

答案 0 :(得分:2)

一些问题:

1)将初始化xml文件结构更改为First Letter CAPS。

# File: app/etc/modules/MyCompanyName_HelloWorld.xml
<?xml version="1.0"?>
<config>
    <modules>
        <MyCompanyName_HelloWorld>
            <active>true</active>
            <codePool>local</codePool>
        </MyCompanyName_HelloWorld>
    </modules>
</config>

2)您引用helloworld/observer作为调用自定义事件的模型,但尚未定义helloworld模型命名空间。将其添加到config.xml块中的<global>

# File: app/code/local/MyCompanyName/HelloWorld/etc/config.xml:
<global>
    ........Your code ...............
    <models>
        <helloworld>
            <class>MyCompanyName_HelloWorld_Model</class>
        </helloworld>
    </models>
    ........Your code ...............
</global>

这样做现在按预期运行(Recoverable Error: Object of class Varien_Event could not be converted to string in /path/to/mage/app/code/local/MyCompanyName/HelloWorld/Model/Observer.php on line 11)。如果将observer方法更改为仅输出Hello World,则可以正常工作。例如:

# File: app/code/local/MyCompanyName/HelloWorld/Model/Observer.php:
<?php                                            
class MyCompanyName_HelloWorld_Model_Observer
{
    public function my_custom_method($observer)
    {
        var_dump('Hello World');
        exit;
    }
}

输出:In index controllerstring(11) "Hello World"