六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 20|回复: 0

I.第一个Magento扩展

[复制链接]

升级  95%

147

主题

147

主题

147

主题

举人

Rank: 3Rank: 3

积分
485
 楼主| 发表于 2013-2-7 17:02:28 | 显示全部楼层 |阅读模式
本文转自 网店专家网 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>


附件下载代码
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表