Magento事件给出错误警告:include(Mage / Modulename / Model / Observer.php):无法打开流

时间:2015-10-16 00:36:47

标签: php magento

日志: 当我点击添加到购物车时,我在 system.log 中得到两行的错误日志:

2015-10-16T00:27:33+00:00 ERR (3): Warning: include(Mage/Modulename/Model/Observer.php): failed to open stream: No such file or directory  in /opt/lampp/htdocs/magento2/lib/Varien/Autoload.php on line 94
2015-10-16T00:27:33+00:00 ERR (3): Warning: include(): Failed opening 'Mage/Modulename/Model/Observer.php' for inclusion (include_path='/opt/lampp/htdocs/magento2/app/code/local:/opt/lampp/htdocs/magento2/app/code/community:/opt/lampp/htdocs/magento2/app/code/core:/opt/lampp/htdocs/magento2/lib:.:/opt/lampp/lib/php')  in /opt/lampp/htdocs/magento2/lib/Varien/Autoload.php on line 94

这是我的名为Namespace_Modulename的插件:

文件: /app/code/local/Namespace/Modulename/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>0.0.1</version>
        </Namespace_Modulename>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <frontend>
        <routers>
          <modulename>
            <use>standard</use>
              <args>
                <module>Namespace_Modulename</module>
                <frontName>modulename</frontName>
              </args>
          </modulename>
        </routers>
    </frontend>
    <global>

        <models>
            <Namespace_Modulename>Namespace_Modulename_Model</Namespace_Modulename>
        </models>

        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <namespace_modulename_checkout_cart_product_add_after_observer>
                        <type>singleton</type>
                        <class>modulename/observer</class>
                        <method>my_custom_method</method>
                    </namespace_modulename_checkout_cart_product_add_after_observer>
                </observers>
            </checkout_cart_product_add_after>
        </events>

    </global>

</config>

文件:/app/code/local/Namespace/Modulename/Model/Observer.php

<?php
/**
 * Our class name should follow the directory structure of
 * our Observer.php model, starting from the namespace,
 * replacing directory separators with underscores.
 * i.e. app/code/local/SmashingMagazine/
 *                     LogProductUpdate/Model/Observer.php
 */
class Namespace_Modulename_Model_Observer
{
    /**
     * Magento passes a Varien_Event_Observer object as
     * the first parameter of dispatched events.
     */
    public function my_custom_method(Varien_Event_Observer $observer)
    {
        // Retrieve the product being updated from the event observer
        $product = $observer->getEvent()->getProduct();

        // Write a new line to var/log/product-updates.log
        $name = $product->getName();
        $sku = $product->getSku();
        file_put_contents("/opt/lampp/htdocs/magento2/test","sdddddddss");
        Mage::log(
            "{$name} ({$sku}) updated",
            null,
            'product-updates.log'
        );
    }
}

文件:app / etc / modules / Namespace_Modulename.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
        <active>true</active>
        <codePool>local</codePool>
        </Namespace_Modulename>
    </modules>
</config>

2 个答案:

答案 0 :(得分:5)

请从

更改config.xml中的以下行

&#13;
&#13;
<class>modulename/observer</class>
&#13;
&#13;
&#13;

&#13;
&#13;
<class>Namespace_Modulename_Model_Observer</class>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

模型的类组(或命名空间)不正确。

您在观察者中调用了<class>modulename/observer</class>,但未将modulename定义为类别组。

<models>
    <Namespace_Modulename>Namespace_Modulename_Model</Namespace_Modulename>
</models>

应该成为

<models>
    <my_unique_class_group>Namespace_Modulename_Model</my_unique_class_group>
</models>

然后您可以使用<class>my_unique_class_group/observer</class>

从工厂开始上课