【问题标题】:Magento Observer generate errors and Magento areas confusingMagento Observer 生成错误和 Magento 区域混乱
【发布时间】:2012-10-31 11:50:27
【问题描述】:

我创建了controller_front_init_routers 事件观察器,它从REST 服务中检索数据以构建菜单。 一切都很好,直到我发现观察者在后端(例如无法保存产品)以及其他服务中产生错误。 我正在为任何结论而苦苦挣扎,所以我提出了一些审问。

  1. 我试图创建一个条件来触发我的观察者 方法,以防我们仅在前端。但是 Magento 认为我们是 总是在前端区域。

(var_dump(Mage::app()->getStore()->isAdmin()) 总是返回 false 和 var_dump(Mage::getDesign()->getArea() == 'adminhtml'))

Can anyone explain what's happened ?
  1. 另外一种解决方案是将事件观察器放在前端 config.xml 中的区域并加载它 Mage::app()->loadArea($this->getLayout()->getArea()); 但我应该去哪里 放置这段代码?在一个新的观察者?这是最 合适的流程?

  2. 这是一种监听事件然后暂停监听器的方法吗? (一旦我的菜单被注册,我就不需要再听事件了)

  3. 使用controller_front_init_routers事件是不是最好的选择?

  4. 谁见过这种问题?

我在 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); 
            }  


    }

【问题讨论】:

    标签: magento observers


    【解决方案1】:

    您应该能够将查询字符串传递给其余服务,类似于您在地址栏中输入它的方式。 Magento 会将其转发给观察者,您可以将其用作标志。

    在您的代码中添加如下内容:

    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');
        }
    
    }
    

    像这样调用你的网站并传递查询字符串

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

    我对 Magento 的新 REST api 不是很熟悉,但我知道它可以在浏览器中运行。也许这个解释可以帮助你。

    【讨论】:

    • 这对您有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多