Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

在Magento中创建一个简单的“Hello World”模块

创建一个模块

Magento的所有添加和自定义都是通过模块完成的。因此,您需要做的第一件事是创建一个新模块。在app/modules命名中创建XML文件,如下所示

cd /path/to/store/app
touch etc/modules/MyCompanyName_HelloWorld.xml
<?xml version="1.0"?>
<config>
     <modules>
        <MyCompanyName_HelloWorld>
            <active>true</active>
            <codePool>local</codePool>
            <!-- 
            /** <codePool> 代码池
            * community 表示:app\code\community
            * local 表示:app\code\local
            * core 表示 app\code\core 
            */
            -->
        </MyCompanyName_HelloWorld>
     </modules>
</config>

MyCompanyName是您修改的唯一命名空间,它不一定是您公司的名称,而是推荐的约定my magento。HelloWorld是您的模块的名称。

清除应用程序缓存

现在模块文件到位了,我们需要让Magento了解它(并检查我们的工作)。在管理应用程序中

  1. 转到系统 - >缓存管理
  2. 从All Cache菜单中选择Refresh
  3. 单击保存缓存设置

现在,我们确保Magento了解该模块

  1. 进入系统 - >配置
  2. 单击高级
  3. 在“禁用模块输出”设置框中,查找名为“MyCompanyName_HelloWorld”的新模块

如果您可以忍受性能下降,则可能需要在开发/学习时关闭应用程序缓存。没有什么比这更令人沮丧的了,忘记清除缓存并想知道为什么你的更改没有出现。

设置目录结构

接下来,我们需要为模块设置目录结构。你不需要所有这些目录,但现在设置它们没有任何害处。

mkdir -p app/code/local/MyCompanyName/HelloWorld/Block
mkdir -p app/code/local/MyCompanyName/HelloWorld/controllers
mkdir -p app/code/local/MyCompanyName/HelloWorld/Model
mkdir -p app/code/local/MyCompanyName/HelloWorld/Helper
mkdir -p app/code/local/MyCompanyName/HelloWorld/etc
mkdir -p app/code/local/MyCompanyName/HelloWorld/sql

并添加配置文件

touch app/code/local/MyCompanyName/HelloWorld/etc/config.xml

在配置文件中,添加以下内容,这实际上是一个“空白”配置。

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompanyName_HelloWorld>
            <version>0.1.0</version>
        </MyCompanyName_HelloWorld>
    </modules>
</config>

这个配置文件过于简单化,可以让你告诉Magento你想要运行什么代码。

设置路由器

接下来,我们需要设置模块的路由器。这将让系统知道我们正在处理任何形式的URL

http://127.0.0.1/magento/index.php/helloworld

因此,在配置文件中,添加以下部分。

<config>
<!-- ... -->
    <frontend>
        <routers>
            <!-- the <helloworld> tagname appears to be arbitrary, but by
            convention is should match the frontName tag below-->
            <helloworld>
                <use>standard</use>
                <args>
                    <module>MyCompanyName_HelloWorld</module>
                    <frontName>helloworld</frontName>
                </args>
            </helloworld>
        </routers>
    </frontend>
<!-- ... -->
</config>

你在这里说的是“任何带有helloworld的frontName的URL ......

http://127.0.0.1/magento/index.php/helloworld

应该使用frontName控制器MyCompanyName_HelloWorld“。

因此,通过上述配置,当您加载上面的helloworld页面时,您将获得404页面。那是因为我们还没有为控制器创建文件。我们现在就这样做。

touch app/code/local/MyCompanyName/HelloWorld/controllers/IndexController.php

现在尝试加载页面。进展!而不是404,你将获得PHP / Magento异常

Controller file was loaded but class does not exist

因此,打开我们刚刚创建的文件,并粘贴以下代码。类的名称需要基于您在路由器中提供的名称。

<?php
class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo "你好,世界!";
    }
}

我们刚刚设置的是模块/ frontName控制器。这是默认控制器和模块的默认操作。如果你想添加控制器或动作,你必须记住,Magento URL的树的第一部分是不可变的,它们总是会这样http://127.0.0.1/magento/index.php/frontName/controllerName/actionName

所以,如果你想匹配这个网址

http://127.0.0.1/magento/index.php/helloworld/foo

你必须有一个FooController,你可以这样做:

touch app/code/local/MyCompanyName/HelloWorld/controllers/FooController.php
<?php
class MyCompanyName_HelloWorld_FooController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo 'Foo Index Action';
    }

    public function addAction(){
        echo 'Foo add Action';
    }

    public function deleteAction(){
        echo 'Foo delete Action';
    }
}

请注意,默认控制器IndexController和默认操作indexAction可以隐式但必须是显式的。

因此http://127.0.0.1/magento/index.php/helloworld/foo将匹配控制器FooController和动作indexAction而不是IndexController的动作fooAction。如果你想拥有一个fooAction,在控制器IndexController中你必须像这样明确地调用这个控制器:http://127.0.0.1/magento/index.php/helloworld/index/foo

因为url的第二部分是并且将永远是controllerName。此行为是Magento中捆绑的Zend Framework的继承。

您现在应该能够点击以下URL并查看echo语句的结果

http://example.com/magento/index.php/helloworld/foo
http://example.com/magento/index.php/helloworld/foo/add
http://example.com/magento/index.php/helloworld/foo/delete

所以,这应该给你一个关于Magento如何调度到控制器的基本想法。从这里开始,我建议在现有的Magento控制器类中查看模型和模板/布局系统的使用方法。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论