2

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.

enter image description here

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • Your module name should be case sensitive, i.e. `MyCompanyName_HelloWorld` rather than all lowercase in your config.xml – scrowler Sep 04 '15 at 03:06
  • also the app/etc/ XML file should be `app/etc/modules/MyCompanyName_HelloWorld.xml` – scrowler Sep 04 '15 at 03:08
  • Robbie sir :) i followed some part from hello world module from here. http://stackoverflow.com/questions/576908/how-to-create-a-simple-hello-world-module-in-magento . And you cant doubt alan storm's solution !! Have a look there ! :) – Pratik Joshi Sep 04 '15 at 03:09
  • I think you'll find that his answer is a typo rather than being wrong. His first example of talking about that file mentions moving to `app` then touching `etc/modules/YourModule_Example.xml` – scrowler Sep 04 '15 at 03:16

1 Answers1

2

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"

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • Well , I will try that out when I reach home ! +1 in advance :) – Pratik Joshi Sep 04 '15 at 04:25
  • `1) Your XML file in app/etc should be app/etc/modules:` Buddy come on, it was just a typo mistake. I already have it in `app/etc/modules`. And if you read the question properly, I said : I already got some part of extension working except dispatcher. Means Extension is working. If xml file was not in app/etc/modules, extension simply would not have worked at all. – Pratik Joshi Sep 04 '15 at 15:34
  • 1
    Glad it worked, but your edit changed the point of the first issue, will adjust shortly – scrowler Sep 04 '15 at 22:14