第一个Magento模块开发

时间:2015-11-14 09:58:21

标签: php xml magento

我是Magento的新人。我正在尝试开发一个自定义模块。我的代码如下

位置: D:\ php \ htdocs \ magento \ app \ etc \ modules

Remote_Mouse.xml

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

位置: D:\ php \ htdocs \ magento \ app \ code \ local \ Remote \ Mouse \ etc   config.xml中

<?xml version="1.0"?>
<config>
    <global>
        <modules>
            <Remote_Mouse>
                <version>0.1.0</version>
            </Remote_Mouse>
        </modules>
        <blocks>
            <mouse>
                <class>Remote_Mouse_Block</class>
            </mouse>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <mouse module="Remote_Mouse">
                    <file>Remote_Mouse.xml</file> 
                </mouse>
            </updates>
        </layout>
        <routers>
            <mouse>
                <use>standard</use>
                <args>
                    <module>Remote_Mouse</module>
                    <frontName>remote</frontName>
                </args>
            </mouse>
        </routers>        
    </frontend>
</config>

位置:

D:\php\htdocs\magento\app\design\frontend\my_theme\default\layout
mouse.xml


<?xml version="1.0"?>
<layout version="0.1.0">
    <reference name="root">
        <action method="setTemplate">
            <template>page/1column.phtml</template>
        </action>
    </reference>
    <mouse_index_index>
        <reference name="content">
            <block type="remote/brush" template="remote/mouse.phtml" />
        </reference>
    </mouse_index_index>
</layout>

位置: D:\php\htdocs\magento\app\code\local\Remote\Mouse\Block

Brush.php

<?php
    class Remote_Mouse_Block_Brush extends Mage_Core_Block_Template
    {
        public function myfunction()
        {
            echo 'mouse';
        }
    }

位置: D:\php\htdocs\magento\app\code\local\Remote\Mouse\controllers

IndexController.php

<?php

class Remote_Mouse_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout(array('default'));
        $this->renderLayout();       
    }

    public function sayHelloAction()
    {
        echo 'Hello one more time...';
    }
}
?>

位置:

d:\ PHP中\ htdocs中\ Magento的\应用\设计\前端\ my_theme \默认\模板\远程

mouse.phtml

<?php
    echo $this->myfunction();
?>

我看不到任何输出。我的代码中有错误吗?

2 个答案:

答案 0 :(得分:1)

如果您尝试运行/remote/index/index操作,则应该出现myfunction function does not exists之类的错误。在您使用的布局文件中

<block type="core/template" name="remote_mouse" template="remote/mouse.phtml" />

代码应该有:

<block type="mouse/mouse" name="remote_mouse" template="remote/mouse.phtml" />

还要确保根据所在位置放置所有文件。如果您没有错误,那么您可能没有在正确的主题模板/布局目录中的模板或布局文件。因此,系统没有找到它,系统没有尝试渲染它并调用myfunction方法,因此没有错误。

还要确保未禁用本地模块的输出。

答案 1 :(得分:1)

您定义的块类型存在以下问题。

<block type="remote/brush" template="remote/mouse.phtml" />

那应该如下。

<block type="mouse/brush" template="remote/mouse.phtml" />