【发布时间】:2012-10-31 11:50:27
【问题描述】:
我创建了controller_front_init_routers 事件观察器,它从REST 服务中检索数据以构建菜单。
一切都很好,直到我发现观察者在后端(例如无法保存产品)以及其他服务中产生错误。
我正在为任何结论而苦苦挣扎,所以我提出了一些审问。
- 我试图创建一个条件来触发我的观察者 方法,以防我们仅在前端。但是 Magento 认为我们是 总是在前端区域。
(var_dump(Mage::app()->getStore()->isAdmin()) 总是返回 false 和 var_dump(Mage::getDesign()->getArea() == 'adminhtml'))
Can anyone explain what's happened ?
另外一种解决方案是将事件观察器放在前端
config.xml中的区域并加载它Mage::app()->loadArea($this->getLayout()->getArea());但我应该去哪里 放置这段代码?在一个新的观察者?这是最 合适的流程?这是一种监听事件然后暂停监听器的方法吗? (一旦我的菜单被注册,我就不需要再听事件了)
使用
controller_front_init_routers事件是不是最好的选择?谁见过这种问题?
我在 Magento 版本上工作。 1.12.0.2
这里是 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>
这里是我的模型观察者中的函数 getEvent
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);
}
}
【问题讨论】: