如何创建不可编辑的Magento CMS页面?


16

在全新安装的Magento上,它带有几个默认的CMS页面,您可以在中进行编辑CMS > Pages。但是,它还带有几个“不可编辑”的CMS页面:Orders and ReturnsContact Us...都是页面上都带有表单的页面,并且突出了Magento CE的一个缺点:创建和编辑表单。

我设法用自己的表格覆盖了默认的“与我们联系”,但是我想添加另一张表格,将来可能需要添加更多的表格。到目前为止,我对创建Magento模块以覆盖现有功能和页面有些熟悉。

我已经开始研究一个模块,该模块将能够在Magento中创建表单页面,但是CMS管理必须像默认表单一样将其隐藏。我找到了以编程方式创建CMS页面的答案,但这将其添加到Magento的CMS > Pages

如何创建只能由Magento模块编辑的CMS页面?


很高兴知道!交叉张贴是犹太洁食还是自从我已经在此发布过以来被接受的惯例?
andyjv

我会点击“标志”链接,并要求国防部为您移动它。一般而言,交叉发布是不受欢迎的。
约翰·孔德

如果要在CMS页面中查找自定义联系表格,请参阅magento.stackexchange.com/questions/79602/…或更详细的stackoverflow.com/q/1066127/664108
Fabian Schmengler,2016年

Answers:


21

实际上,“联系我们”和“订单和退货”不是CMS页面。它们实际上是来自单独模块的页面。它们更像是“登录”或“注册”页面,而不是CMS页面。要创建这样的页面,您可以创建一个带有控制器,一个块和一个模板的简单模块。让我们将扩展名为Easylife_Customform。为此,您将需要以下文件。
app/etc/modules/Easylife_Customform.xml-模块声明文件

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

app/code/local/Easylife/Customform/etc/config.xml -配置文件

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Customform>
            <version>0.0.1</version>
        </Easylife_Customform>
    </modules>
    <global>
        <blocks>
            <customform><!-- block alias -->
                <class>Easylife_Customform_Block</class>
            </customform>
        </blocks>
        <helpers>
            <customform><!-- helper alias -->
                <class>Easylife_Customform_Helper</class>
            </customform>
        </helpers>
    </global>
    <frontend>
        <routers>
            <customform>
                <use>standard</use>
                <args>
                    <module>Easylife_Customform</module>
                    <frontName>customform</frontName><!-- url key for module -->
                </args>
            </customform>
        </routers>
        <layout>
            <updates>
                <easylife_customform>
                    <file>easylife_customform.xml</file><!-- frontend layout file -->
                </easylife_customform>
            </updates>
        </layout>
        <translate>
            <modules>
                <Easylife_Customform>
                    <files>
                        <default>Easylife_Customform.csv</default><!-- translation file (not mandatory) -->
                    </files>
                </Easylife_Customform>
            </modules>
        </translate>
    </frontend>
</config>

app/design/frontend/base/default/layout/easylife_customform.xml -前端布局文件

<?xml version="1.0"?>
<layout>
    <customform_index_index translate="label" module="customform">
        <label>Custom form</label>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action><!-- can be different -->
        </reference>        
        <reference name="content">
            <block type="core/template" name="customform" template="easylife_customform/form.phtml" /><!-- content of page -->
        </reference>
    </customform_index_index>
</layout>

app/code/local/Easylife/Customform/Helper/Data.php -默认模块助手

<?php
class Easylife_Customform_Helper_Data extends Mage_Core_Helper_Abstract{
}

app/design/frontend/base/default/template/easylife_customform/form.phtml -表单的实际html-使其看起来像您需要的

<form action="<?php echo $this->getUrl('customform/index/send')?>">
    <input type="text" name="name" />
    <input type="submit" />
</form>

app/code/local/Easylife/Customform/controllers/IndexController.php -模块控制器

<?php 
class Easylife_Customform_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){ //this will display the form
        $this->loadLayout();
        $this->_initLayoutMessages('core/session'); //this will allow flash messages
        $this->renderLayout();
    }
    public function sendAction(){ //handles the form submit
        $post = $this->getRequest()->getPost();
        //do something with the posted data
        Mage::getSingleton('core/session')->addSuccess($this->__('Your message was sent'));//add success message.
        $this->_redirect('*/*');//will redirect to form page
    }
}

应该是这样。清除缓存,您应该能够访问该表单,mysite.com/customform
希望我正确编写了代码并且没有错过任何内容


2
您在这个答案上确实付出了更多的努力。+1
philwinkle

@philwinkle:是好是坏?:)
Marius

真的很棒的导游Marius,谢谢!我正在尝试设置页面标题,布局xml中的<label>被忽略了,这 <reference name="head"> <action method="setTitle" translate="title"><title>Subscribe to our Newsletter</title></action> </reference> 将不起作用。
loeffel

@loeffel。也许您还有其他东西可以覆盖标题。理论上,代码应该可以工作。
Marius

@Marius这非常方便,但是如何添加错误消息?我尝试添加,Mage::getSingleton('core/session')->addError("Error");但没有运气。它仅显示成功消息。有什么帮助吗?
阿米尔·西迪克
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.