Custom Event In Magento

由于过于简单遂未翻译。
To dispatch an event, use the Mage::dispatchEvent function (/app/Mage.php). Calls look like this:

1 $data = array( 'somedata' => 'foo', 'layout' => $this->getLayout());
2 Mage::dispatchEvent('my_event_name', $data);

In order to observe an event, specify the observer in the config.xml file of your extension.

 1 <config>
 2     <global>
 3         <events>
 4             <my_event_name>
 5                 <observers>
 6                     <myextension>
 7                         <type>singleton</type>
 8                               <class>myextension/observer</class>
 9                         <method>someMethodName</method>
10                     </myextension>
11                 </observers>
12             </my_event_name>
13         </events>
14     </global>
15 </config>

Create a corresponding class and method (Observer.php) and you’re set to go:

1 class Myextension_Model_Observer {
2     public function someMethodName($event) {
3         $layout = $event->getLayout();
4         $someData = $event->getSomedata();
5     }
6 }

 

 

你可能感兴趣的:(Custom Event In Magento)