Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

In app/code/core/Mage/Customer/conrtrollers/AccountController.php there is

Mage::dispatchEvent('customer_register_success',
            array('account_controller' => $this, 'customer' => $customer)
        );

How this event code i.e customer_register_success is fired in magento when dispatchEvent is called.

share|improve this question
1  
question is not clear....breify in details –  Amit Bera May 15 at 6:16
    

1 Answer 1

up vote 2 down vote accepted

If you look into the function dispatchEvent in the class Mage_Core_Model_App you will see that the code works as follows:

NOTE: for this example I will use the event controller_front_init_routers simply because this happens a lot and is easier to step through the code with

Firstly what happens is that all the observers set-up in the config are loaded into an array of the format:

$observers['observer_name'] = array(
    'type'  => 'observer_type,
    'model' => 'observer_model',
    'method'=> 'method_to_call',
    'args'  => 'arguments_array',
);

This matches the way you define an observer via xml as follows.

<events>
    <event_name>
        <observers>
            <observer_name>
                <type>observer_type</type>
                <class>observer_model</class>
                <method>method_to_call</method>
            </observer_name>
        </observers>
    </event_name>
</events>

For our example the array looks as follows.

array(1) {
  ["cms"]=>
  array(4) {
    ["type"]=>
    string(0) ""
    ["model"]=>
    string(26) "Mage_Cms_Controller_Router"
    ["method"]=>
    string(21) "initControllerRouters"
    ["args"]=>
    array(0) {
    }
  }
}

Then what happens is the dispatchEvent function checks to see if the event we want to dispatch has something listening to it. If it does not then it wont continue any further but when it does it continues to build an event object and observer object.

It then loops through all the stored event information, sets the data arguments and calls the desired method on the desired object. Note that by default the model is loaded as a singleton.

foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
    $observer->setData(array('event'=>$event));
    Varien_Profiler::start('OBSERVER: '.$obsName);
    switch ($obs['type']) {
        case 'disabled':
            break;
        case 'object':
        case 'model':
            $method = $obs['method'];
            $observer->addData($args);
            $object = Mage::getModel($obs['model']);
            $this->_callObserverMethod($object, $method, $observer);
            break;
        default:
            $method = $obs['method'];
            $observer->addData($args);
            $object = Mage::getSingleton($obs['model']);
            $this->_callObserverMethod($object, $method, $observer);
            break;
    }
    Varien_Profiler::stop('OBSERVER: '.$obsName);
}

One final thing that happens is that the _callObserverMethod function checks to see if the desired method exists and when it does not it throws an exception.

So for our example we will be using the model Mage_Cms_Controller_Router and the method initControllerRouters.

share|improve this answer
    
Thanks.Nice explanation –  Abinaya May 15 at 9:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.