2
votes

I've made controller_front_init_routers event observer, which retrieves data from a REST service to construct a menu. All was ok until I discovered that the observer generates errors in the backend (no way to save products for example) and also in the rest services. I am struggling for any conclusions, So I raised some interrogations.

  1. I tried to make a condition in order to trigger my Observer methods in case we are in frontend only. But Magento considers that we are always in frontend area.

(var_dump(Mage::app()->getStore()->isAdmin()) return always false and the same with var_dump(Mage::getDesign()->getArea() == 'adminhtml'))

Can anyone explain what's happened ?
  1. Also one solution is to place the event observer in frontend area in the config.xml and to load it with Mage::app()->loadArea($this->getLayout()->getArea()); but where should I place this piece of code ? in a new observer ? Is that the most appropriate process ?

  2. Is it a way to listen once an event then to pause the listener? (once my menu is registered, I don't need to listen to the event any more)

  3. Is the use of controller_front_init_routers event the best choice ?

  4. Who has ever seen that kind of problem ?

I work on Magento ver. 1.12.0.2

Here the config.xml

<globals>
....
<events>
<controller_front_init_routers>
<observers>
  <connector_services_observer>
    <type>singleton</type>
    <class>Connector_Services_Model_Observer</class>
      <method>getEvent</method>
       </connector_services_observer>
  </observers>
</controller_front_init_routers>        
</events>
</globals>

Here the function getEvent in my model observer

public function getEvent($observer){

    //model which do post or get requests and return xml and menu
    $_getRest = Mage::getModel('connector_services/RestRequest');
    //the paths
    $_menu_url = Mage::getStoreConfig('connector_service_section/connector_service_url/service_menu_url');
    //put a store config
    $path_nlist = 'veritas-pages-list.xml';

    $_isAdmin = Mage::helper('connector_services');

    $currentUrl=Mage::helper("core/url")->getCurrentUrl();
    //the way I found to trigger methods only in frontend
            //that's not very beautiful I know
        $admin = preg_match("#/admin/#",$currentUrl);
        $api =  preg_match("#/api/#",$currentUrl);
    //
    if ( !$admin && ! $api ){


            $_menuXml = $_getRest->postRequest($_menu_url);

            if( $_menuXml )
            { 
                $_menu = $_getRest->makeMenu($_menuXml);
                Mage::register('menu',$_menu); 
            }  


    }
1

1 Answers

1
votes

You should be able to pass a querystring to the rest service, similar to how you'd just type it out in the address bar. Magento will forward it on to the observer, and you can use it as a flag.

Add something like the following to your code:

const USE_FRONTEND = 'usefront';

public function getEvent($observer){
    this->request = $observer->getEvent()->getData('front')->getRequest();

    // If the constant is in the query string 
    if ($this->request->{self::USE_FRONTEND}) {
        // Do code related to this request
        die('Frontend flag detected');
    }

}

Call to your site like this and pass the querystring

http://www.yourmagentosite.com/?usefront=true

I am not extremely familiar with Magento's new REST api, but I know it works in the browser. Maybe this explanation can help you out.