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