Reputation: 11693
I tried the same code from this site.
It works well but the code for observer doesnt seem to be working.
I mean in observer method I have echoed some text and used exit()
too. But the control doesnt go there. I tried to debug a lot, but couldnt get the solution.
Thanks in advance.
This is screen shot of directory structure of my module.
app/etc/MyCompanyName_HelloWorld.xml
<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</MyCompanyName_HelloWorld>
</modules>
</config>
Now my module files
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";
}
}
And output I am getting (Without magento executing my observer echo statement)
In index controller --after dispatch
But it was supposed to print Hi i am inside event
text too written in observer.
Upvotes: 2
Views: 1735
Reputation: 24406
Couple of problems:
1) Change your initialization xml file structure to 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) You're referencing helloworld/observer
as the model to call on your custom event, but haven't defined the helloworld
model namespace. Add this to your config.xml
in the <global>
block:
# File: app/code/local/MyCompanyName/HelloWorld/etc/config.xml:
<global>
........Your code ...............
<models>
<helloworld>
<class>MyCompanyName_HelloWorld_Model</class>
</helloworld>
</models>
........Your code ...............
</global>
Doing that it now runs as expected (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
). If you change the observer method to just output Hello World, it works fine. For example:
# 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;
}
}
Output: In index controllerstring(11) "Hello World"
Upvotes: 2