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.
- 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 ?
Also one solution is to place the event observer in frontend area in the
config.xml
and to load it withMage::app()->loadArea($this->getLayout()->getArea());
but where should I place this piece of code ? in a new observer ? Is that the most appropriate process ?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)
Is the use of
controller_front_init_routers
event the best choice ?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);
}
}