2

I am new to magneto.I have local module and it has a observer(it has function). I need to call that observer function via url to check whether its working or not.

Config.xml

<config>
   <modules>
       <Magestore_Lesson17>
           <version>0.1.0</version>
       </Magestore_Lesson17>
   </modules>  
   <global>
       <models>
           <lesson17>
               <class>Magestore_Lesson17_Model</class>
           </lesson17>
       </models>
       <helpers>
           <lesson17>
               <class>Magestore_Lesson17_Helper</class>
           </lesson17>
       </helpers>
       <events>
           <checkout_onepage_controller_success_action>
               <observers>
                   <magestore_lesson17_obsever>
<type>singleton</type>
                       <class>lesson17/observer</class>
                       <method>getAlert</method>
                   </magestore_lesson17_obsever>
               </observers>
            </checkout_onepage_controller_success_action>
       </events>       
</config> 

Observer.php

<?php
class Magestore_Lesson17_Model_Observer
{

        public function getAlert($observer)
        {
           echo "Hello world";
        }
}
?>
1
  • 3
    you cannot call observers via url. You need a controller and an action to be able to call it.
    – Marius
    Commented Feb 24, 2016 at 7:54

3 Answers 3

1

I don't get your motive behind calling a observer method using url. But yes you can do that by using controller action method, which is called when url is hit is broswer. The controller method will in turn call your observer method.

Below is modified version of your config.xml, which adds a frontend router -

<config>
   <modules>
       <Magestore_Lesson17>
           <version>0.1.0</version>
       </Magestore_Lesson17>
   </modules>  

   <!-- Adding frontend router so your controller action method gets called when url is hit -->

   <frontend>
     <routers>
        <routeurfrontend>
            <use>standard</use>
            <args>
               <module>Magestore_Lesson17</module>
               <frontName>lesson17</frontName>
            </args>
         </routeurfrontend>
     </routers>
  </frontend>

   <global>
       <models>
           <lesson17>
               <class>Magestore_Lesson17_Model</class>
           </lesson17>
       </models>
       <helpers>
           <lesson17>
               <class>Magestore_Lesson17_Helper</class>
           </lesson17>
       </helpers>
       <events>
           <checkout_onepage_controller_success_action>
               <observers>
                   <magestore_lesson17_obsever>
                      <type>singleton</type>
                       <class>lesson17/observer</class>
                       <method>getAlert</method>
                   </magestore_lesson17_obsever>
               </observers>
            </checkout_onepage_controller_success_action>
       </events>
    </global>
</config> 

Now next step is to create your controller class and method.

Add "controllers" folder in Magestore/Lesson17/. Create IndexController.php file and add below code -

<?php
class Magestore_Lesson17_IndexController extends Mage_Core_Controller_Front_Action{ 

    public function indexAction(){
        Mage::getModel('lesson17/observer')->getAlert();
    } 
}

Once this is done. Clear var/cache folder and hit the url -

www.sitename.com/lesson17/index/

And your observer method is called.

Hope this helps. This is same method as mentioned by zhartaunik, but this is just in detail explaination.

1

There is an easier way to check whether your observer function is being called or not.

Just make use of magento's log functionality.

For example:

<?php
class Magestore_Lesson17_Model_Observer
{
        public function getAlert($observer)
        {
           //echo "Hello world";
           Mage::log('Hello world');
        }
}
?>

If you have enabled magento's log from System > Configuration > Advanced (Developer) > Log Settings, then above code will print Hello world in /var/log/system.log file.

0

I'd recommend you to call it from some of your or core controllers. For example there is /app/code/core/Catalog/controllers/ProductController

You may add new action for this. E.g.

public function testAction()
{
     Mage::getModel('lesson17/observer')->getAlert();
}

To call this action you need to run in browser:

http://your_local_url/catalog/product/test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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