Magento 调度自定义事件不起作用
Magento dispatch custom event not working
我尝试了来自 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 执行我的观察者 echo 语句)
In index controller --after dispatch
但它应该打印 Hi i am inside event
文本太用观察者写的。
几个问题:
1) 将您的初始化 xml 文件结构更改为首字母大写。
# 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
模型命名空间。将此添加到 <global>
块中的 config.xml
:
# 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
)。如果您将观察者方法更改为仅输出 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"
我尝试了来自 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 执行我的观察者 echo 语句)
In index controller --after dispatch
但它应该打印 Hi i am inside event
文本太用观察者写的。
几个问题:
1) 将您的初始化 xml 文件结构更改为首字母大写。
# 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
模型命名空间。将此添加到 <global>
块中的 config.xml
:
# 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
)。如果您将观察者方法更改为仅输出 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"