Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've made an controller_front_init_routers event observer which retrieve datas from a rest service to construct a menu. All was ok until I discover this observer generate errors in the backend (no way to save products for example) and also in the rest services. Struggling for the problem 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 consider we are always in frontend aera. (var_dump(Mage::app()->getStore()->isAdmin()) return always false and the same with var_dump(Mage::getDesign()->getArea() == 'adminhtml')). Do anyone can explains what's happened ?

  2. It's also one solution is to place the event observer in frontend aera in the config.xml and to load it with Mage::app()->loadArea($this->getLayout()->getArea()); but where I place this piece of code ? in a new observer ? Is that the most appropriate process ?

  3. Is it a way to listen once an event then to pause the listener. (once my menu is registred, I don't need to listen to the event anymore)

  4. Is The use of controller_front_init_routers event the best choice ?

  5. 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); 
            }  


    }
share|improve this question
add comment

1 Answer

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.

share|improve this answer
 
Was this helpful in any way? –  CarComp Jan 8 at 20:16
 
Long time I didn't went here... thanks for your answer.. –  Robert Mar 6 at 9:04
add comment

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.