koda 发表于 2013-2-7 17:02:28

I.第一个Magento扩展

本文转自 网店专家网 cartz.com.cn
http://www.cartz.com.cn/bbs/topic-t53.html

Magento扩展开发的概念相对统一,名称叫Module.
从访问入口看,Magento扩展一般有两种展现形式:
1. 作为一个完整的页面,从浏览器地址栏访问新增的模块。
2. 作为页面的一个片段,插接到现已存在的网页上。

下面的例子描述的第一种方式扩展出来的模块。
案例假设公司名称为Cartz, 现在要为该公司编写一个旅馆预订的模块。
步骤 I: 建立模块目录及文件结构骨架。
目录结构

    【Magento】       app          - etc/            - modules/                Cartz_Hotel.xml          - code/            - local/               - Cartz/                  - Hotel/                      - controllers                                           MyController.php                      - etc                        config.xml

步骤 II: Cartz_Hotel.xml

    <config>      <modules>         <Cartz_Hotel>            <active>true</active>            <codePool>local</codePool>            <version>0.1.0</version>         </Cartz_Hotel>      </modules>    </config>

该文件的目的是让Magento系统载入该模块。<active>标签为true表示使该模块生效。注意标签<Cartz_Hotel>是命名约定,从而约束了你的代码应该放在app/code/Cartz/Hotel目录下。

步骤 III: MyController.php
    <?php    class Cartz_Hotel_MyController extends Mage_Core_Controller_Front_Action{      public function helloAction() {          echo "My First Module";      }    }

类名构成: 前缀(Cartz_Hotel)加上文件的名字(IndexController),然后要求扩展基类 Mage_Core_Controller_Front_Action。
稍后我们要看看如何调用indexAction方法来输出“My First Module”.

步骤 IV: config.xml

      <?xml version="1.0"?>    <config>      <modules>            <Cartz_Hotel>                <version>0.1.0</version>            </Cartz_Hotel>      </modules>      <frontend>            <routers>                <hotel>                  <use>standard</use>                  <args>                        <module>Cartz_Hotel</module>                        <frontName>hotel</frontName>                  </args>                </hotel>            </routers>      </frontend>    </config>

frontend/routers/用来设置使该模块从前端显示的入口。frontName稍后将出现在 url中

假设Magento在本机安装的访问首页是http://localhost/magento/index.php, 那么在浏览器地址栏中输入
http://localhost/magento/index.php/hotel/my/hello
将在页面输入"My First Module"

从而得出Magento模块url访问的命名规范
http://<host>/<Magento虚拟目录>/<config.xm中的frontName>/<Controller文件名去掉Controller>/<Controller文件的方法名去掉Action>


附件下载代码
页: [1]
查看完整版本: I.第一个Magento扩展