【问题标题】:Magento 1.6.2 - Problems overriding contacts controllerMagento 1.6.2 - 覆盖联系人控制器的问题
【发布时间】:2012-05-04 17:57:11
【问题描述】:

好的,所以我设置了一个新模块来覆盖联系人控制器,以便我可以向它添加新闻通讯注册选项。我的设置如下:

/app/code/local/MyNamespace/ContactsPlus/controllers/Contacts/IndexController.php:

<?php
# Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Contacts/controllers/IndexController.php';
class MyNameSpace_ContactsPlus_Contacts_IndexController extends Mage_Contacts_IndexController
{
    # Overloaded indexAction
    public function indexAction() {
        # Just to make sure
        error_log('Yes, I did it!');
        parent::indexAction();
    }
}

/app/code/local/MyNamespace/ContactsPlus/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <mynamespace_ContactsPlus>
            <version>0.1.0</version>
        </mynamespace_ContactsPlus>
    </modules>
    <global>
        <rewrite>
            <mynamespace_contactsplus_contacts_index>
                <from><![CDATA[#^/contacts/index/#]]></from>
                <to>/contactsplus/contacts_index/</to>
            </mynamespace_contactsplus_contacts_index>
            <mynamespace_contactsplus_contacts_index>
                <from><![CDATA[#^/contacts/#]]></from>
                <to>/contactsplus/contacts_index/</to>
            </mynamespace_contactsplus_contacts_index>            
        </rewrite>
    </global>
    <frontend>
        <routers>
            <mynamespace_contactsplus>
                <use>standard</use>
                <args>
                    <module>mynamespace_ContactsPlus</module>
                    <frontName>contactsplus</frontName>
                </args>
            </mynamespace_contactsplus>
        </routers>
    </frontend>    
</config>

/app/etc/modules/MyNamespace_All.xml:

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

该模块出现在管理模块列表中,并且在我的 /contacts/ 页面上产生了以下错误:

Fatal error: Call to a member function setFormAction() on a non-object in /srv/www/foo.com/app/code/core/Mage/Contacts/controllers/IndexController.php on line 54 

就是这一行:

        $this->getLayout()->getBlock('contactForm')->setFormAction( Mage::getUrl('*/*/post') );

我不确定从这里去哪里,猜测是它无法对从 Mage::getUrl('//post' 返回的任何内容设置表单操作) 但我正抓着稻草。

任何建议的帮助将不胜感激!

【问题讨论】:

    标签: magento


    【解决方案1】:

    好的,经过大量研究、帮助和普遍的挫败感,这就是我如何让它工作的:

    首先,我的模块目录设置如下(注意目录上的大写):

    /app/code/local/MyNamespace/ContactsPlus/etc/

    • config.xml

    /app/code/local/MyNamespace/ContactsPlus/controllers/

    • IndexController.php

    /app/code/local/MyNamespace/ContactsPlus/Helper/

    • 数据.php

    现在是配置文件:

    /app/code/local/MyNamespace/ContactsPlus/etc/config.xml:

    <?xml version="1.0"?>
    <config>
        <modules>
            <MyNameSpace_ContactsPlus>
                <version>0.1.0</version>
            </MyNameSpace_ContactsPlus>
        </modules>
        <frontend>
            <routers>
    
            <!-- Creates route to my module via /contactsplus/ - I used this for testing -->
                <contactsplus>
                    <use>standard</use>
                    <args>
                        <module>MyNameSpace_ContactsPlus</module>
                        <frontName>contactsplus</frontName>
                    </args>
                </contactsplus>
    
            <!-- Sets Mage_Contacts route to MyNameSpace_ContactsPlus -->              
                <contacts>
                    <args>
                        <modules>
                            <MyNameSpace_ContactsPlus before="Mage_Contacts">MyNameSpace_ContactsPlus</MyNameSpace_ContactsPlus>
                        </modules>
                    </args>
                </contacts> 
            </routers>
        <!-- Sets layout config file (essential for this to work) -->   
            <layout>
                <updates>
                    <contactsplus>
                        <file>contactsplus.xml</file>
                    </contactsplus>
                </updates>
            </layout>        
        </frontend>
        <global>
        <!-- Sets a helper class for the module, when overriding contacts this is also essential. -->   
            <helpers>
                <contactsplus>
                    <class>MyNameSpace_ContactsPlus_Helper</class>
                </contactsplus>
            </helpers>        
        </global>
    </config>
    

    /app/code/local/MyNamespace/ContactsPlus/controllers/Contacts/IndexController.php:

    <?php
    # Controllers are not autoloaded so we will have to do it manually:
    require_once 'Mage/Contacts/controllers/IndexController.php';
    class MyNameSpace_ContactsPlus_IndexController extends Mage_Contacts_IndexController
    {
        # Overloaded indexAction
        public function indexAction() {
            # Just to make sure
            //die('Yes, I did it!');
            parent::indexAction();
        }
    }
    

    /app/code/local/MyNamespace/ContactsPlus/Helper/Data.php:

    <?php
    class MyNameSpace_ContactsPlus_Helper_Data extends Mage_Core_Helper_Abstract
    {
    
    }
    

    /app/etc/modules/MyNamespace_ContactsPlus.xml:

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

    /app/design/frontend/mythemepackage/mytheme/layout/contacts.xml:

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <default>
            <reference name="footer_links">
                <!-- <action method="addLink" translate="label title" module="contacts" ifconfig="contacts/contacts/enabled"><label>Contact Us</label><url>contacts</url><title>Contact Us</title><prepare>true</prepare></action>
             --></reference>
        </default>
        <contacts_index_index translate="label">
        <!-- had to comment this out in order to prevent a duplicate form issue, if anyone has a better method for this then I'd love to here it :) 
            <label>Contact Us Form</label>
            <reference name="head">
                <action method="setTitle" translate="title" module="contacts"><title>Contact Us</title></action>
            </reference>
            <reference name="root">
                <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
                <action method="setHeaderTitle" translate="title" module="contacts"><title>Contact Us</title></action>
            </reference>
            <reference name="content">
                <block type="core/template" name="contactForm" template="contacts/form.phtml"/>
            </reference>
        -->
        </contacts_index_index>
    
        <!-- added this to rewrite contacts handle to the new modules handle -->
        <contacts_index_index>
            <update handle="contactsplus_index_index"/>
        </contacts_index_index>
    </layout>
    

    /app/design/frontend/mythemepackage/mytheme/layout/contactsplus.xml:

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <default>
            <reference name="footer_links">
                <!-- <action method="addLink" translate="label title" module="contacts" ifconfig="contacts/contacts/enabled"><label>Contact Us</label><url>contacts</url><title>Contact Us</title><prepare>true</prepare></action>
             --></reference>
        </default>
        <contactsplus_index_index translate="label">
            <label>Contact Us Form</label>
            <reference name="head">
                <action method="setTitle" translate="title" module="contactsplus"><title>Contact Us</title></action>
            </reference>
            <reference name="root">
                <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
                <action method="setHeaderTitle" translate="title" module="contactsplus"><title>Contact Us</title></action>
            </reference>
            <reference name="content">
                <block type="core/template" name="contactForm" template="contactsplus/custom_form.phtml"/>
            </reference>
        </contactsplus_index_index>
    
    </layout>
    

    我也复制了一份/app/design/frontend/mythemepackage/mytheme/template/contacts/form.phtml 放到/app/design/frontend/mythemepackage/mytheme/template/contactsplus/ 然后修改以满足我的要求。

    在此过程中我发现特别有用的资源是 google、IRC #magento 和

    http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table

    http://alanstorm.com

    希望这可以在某个时候对其他人有所帮助。

    现在是在我的新表单中添加时事通讯注册选项!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多