1.下面介绍的前提是你已经安装了magento ,版本是1.9.1.0。

2.下面是实际步骤

①在工程下面创建下面的文件目录

app/code/local/Magentotutorial/Helloworld/Block
app/code/local/Magentotutorial/Helloworld/controllers
app/code/local/Magentotutorial/Helloworld/etc
app/code/local/Magentotutorial/Helloworld/Helper
app/code/local/Magentotutorial/Helloworld/Model
app/code/local/Magentotutorial/Helloworld/sql
其中 ②

create a configuration file for the module (at path app/code/local/Magentotutorial/Helloworld/etc/config.xml):

<config>
<modules>
<Magentotutorial_Helloworld>
<version>0.1.0</version>
</Magentotutorial_Helloworld>
</modules>
</config>

Then create a file to activate the module (at path app/etc/modules/Magentotutorial_Helloworld.xml):

<config>
<modules>
<Magentotutorial_Helloworld>
<active>true</active>
<codePool>local</codePool>
</Magentotutorial_Helloworld>
</modules>
</config>

Finally, we ensure the module is active:

  1. Clear your Magento cache.
  2. In the Magento Admin, go to System->Configuration->Advanced.
  3. Expand "Disable Modules Output" (if it isn't already).
  4. Ensure that Magentotutorial_Helloworld shows up.

以上解释一下:

Module(模块)的全称是Magentotutorial_Helloworld,模块也可以是Helloworld

⑤配置路由

路由访问路径:

http://example.com/frontName/actionControllerName/actionMethod/

In your config.xml file(at path app/code/local/Magentotutorial/Helloworld/etc/config.xml), add the following section:

<config>
...
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>Magentotutorial_Helloworld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
...
</config>
下面解释一下:

frontend我理解为前端的,是在地址栏中能看到的。

frontName就是helloworld,全是小写。

为什么有个标签是helloworld呢?This tag should be the lowercase version of you module name. Our module name is Helloworld, this tag is helloworld. Technically this tag defines our route name

这个标签应该是模块名的小写,我们的模块名是Helloworld,这里就要写helloworld,这个就定义了路由名称。

注意:

You'll also notice our frontName matches our module name. It's a loose convention to have frontNames match the module names, but it's not a requirement. In your own modules, it's probably better to use a route name that's a combination of your module name and package name to avoid possible namespace collisions.

你可以看到frontName跟module name是搭配的,仅仅是一个宽松的联系,但这并不是要求,在我们自己写的模块中,最好用的路由名是结合module名与包名。避免命名冲突。

What's <module>Magentotutorial_Helloworld</module> for?

This module tag should be the full name of your module, including its package/namespace name. This will be used by the system to locate your Controller files.

注:Magentotutorial可以理解为包名package/namespace name,被系统用来定位控制器文件的。

⑥Create Action Controller(s) for our Routes

为路由创建控制器

新建

app/code/local/Magentotutorial/Helloworld/controllers/IndexController.php 
代码:
<?php
class Magentotutorial_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
echo 'Hello World';
}
}
⑦清空缓存,访问如下url:
http://example.com/helloworld/index/index

http://example.com/helloworld/index/
http://example.com/helloworld/
看到Hello World就说明控制器成功了。
注:控制器php文件必须放在模块的controllers文件夹下面,系统将会按照路径去找。
控制器类如何命名:

An Action Controller's name will

  1. Start with this string specified in config.xml (Magentotutorial_Helloworld)
  2. Be followed by an underscore (Magentotutorial_Helloworld_)
  3. Which will be followed by the Action Controller's name (Magentotutorial_Helloworld_Index)
  4. And finally, the word "Controller" (Magentotutorial_Helloworld_IndexController)

All Action Controllers need Mage_Core_Controller_Front_Action as an ancestor.

模块全称_控制器名字(IndexController.php 中的IndexController),Mage_Core_Controller_Front_Action这个类是控制器的祖宗类。

http://example.com/frontName/actionControllerName/actionMethod/  访问方式。
the URI portion "helloworld" is the frontName, which is followed by index (The Action Controller name),
which is followed by another index, which is the name of the Action Method that will be called.
(an Action of index will call the method public function indexAction(){...}.
要学会 根据路径找方法:
根据路径找方法:
http://example.com/checkout/cart/add

Magento would

  1. Consult the global config to find the module to use for the frontName checkout (Mage_Checkout)
  2. Look for the cart Action Controller (Mage_Checkout_CartController)
  3. Call the addAction method on the cart Action Controller

先找全局config.xml找checkout这个frontName使用地方,找到cart控制器 ,找到控制器类中addAction这个方法。

还有一种方法:
public function paramsAction() {
echo '';
foreach($this->getRequest()->getParams() as $key=>$value) { echo 'Param: '.$key.''; echo 'Value: '.$value.''; } echo '';
}
访问这个地址http://example.com/helloworld/index/params?foo=bar&baz=eof
你会看到值会打印出来。
总结:
这篇文章主要用helloworld的例子讲解了magento的模块,模块结构,以及控制器的命名,控制器中的的方法怎么调用及反过来根据地址栏找相应的方法。

备注:学习资料就是magento官方文档。

链接:http://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-1.html

以helloworld为例讲解magento中控制器的工作的更多相关文章

  1. 第九节:详细讲解Java中的泛型,多线程,网络编程

    前言 大家好,给大家带来详细讲解Java中的泛型,多线程,网络编程的概述,希望你们喜欢 泛型 泛型格式:ArrayList list= new ArrayList(); ArrayList list= ...

  2. 举例讲解Python中的死锁、可重入锁和互斥锁

    举例讲解Python中的死锁.可重入锁和互斥锁 一.死锁 简单来说,死锁是一个资源被多次调用,而多次调用方都未能释放该资源就会造成死锁,这里结合例子说明下两种常见的死锁情况. 1.迭代死锁 该情况是一 ...

  3. 以python为例讲解闭包机制

    以python为例讲解闭包机制 缘起 在学习JS的过程中,总是无可避免的接触到闭包机制,尤其是接触到react后,其函数式的编程思想更是将闭包发扬光大,作为函数式编程的重要语法结构,python自然也 ...

  4. 从零开始讲解JavaScript中作用域链的概念及用途

    从零开始讲解JavaScript中作用域链的概念及用途 引言 正文 一.执行环境 二.作用域链 三.块级作用域 四.其他情况 五.总结 结束语 引言 先点赞,再看博客,顺手可以点个关注. 微信公众号搜 ...

  5. 详细讲解nodejs中使用socket的私聊的方式

    详细讲解nodejs中使用socket的私聊的方式 在上一次我使用nodejs+express+socketio+mysql搭建聊天室,这基本上就是从socket.io的官网上的一份教程式复制学习,然 ...

  6. 在magento中如何回复客户的评论

    magento — 在magento中如何回复客户的评论 发表于 2012 年 8 月 18 日 agento本身是不带 回复评论的功能的,现成的扩展(无论免费的还是商业的)也没找到,那就自己写一个吧 ...

  7. Magento中,调用静态块的几种方法

    在后台创建一个order_form静态块Block Title :Order FormIdentifier :order_formStatus :EnabledContent :自定义内容 1.如果要 ...

  8. MVC4.0 解决Controllers与Areas中控制器不能同名问题

    在使用MVC4.0的时候,难免会遇到在根目录下的Controllers中添加的控制器名称可能会跟在Areas中的某个区域下的控制器名称一样.这个时候访问Areas下面的Controller/Actio ...

  9. iPad中控制器view的width和height

    一.iPad中控制器view的width和height 1> 规律 * width 是宽高中最小的那个值 * height 是宽高中最大的那个值 2> 举例(比如窗口根控制器的view,有 ...

随机推荐

  1. c语言中逗号运算符和逗号表达式

    原文:c语言中逗号运算符和逗号表达式 C语言提供一种特殊的运算符——逗号运算符.用它将两个表达式连接起来.如: 3+5,6+8称为逗号表达式,又称为“顺序求值运算符”.逗号表达式的一般形式为 表达式1 ...

  2. mongodb操作之使用javaScript实现多表关联查询

    一.数据控制 mongodb操作数据量控制,千万控制好,不要因为操作的数据量过多而导致失败. 演示一下发生此类错误的错误提示:

  3. CSS背景图片定位

    原文:CSS背景图片定位 在网页开发中我们经常需要对图片进行分割(如下图)来使用,而不是分别提供单独的图片来调用,常见的如页面背景,按钮图标等,这样做的好处就是减少请求次数,节省时间和带宽. 对背景图 ...

  4. java 转成字符串 json 数组和迭代

    当你需要转成一串一串的json 排列 .当内容和遍历它们. 首页进口 net.sf.json.JSONArray和net.sf.json.JSONObject 两个jar 包 String str = ...

  5. 【转载】Android中ListView下拉刷新的实现

    在网上看到一个下拉刷新的例子,很的很棒,转载和更多的人分享学习 原文:http://blog.csdn.net/loongggdroid/article/details/9385535 ListVie ...

  6. Windows下MYSQL自动备份批处理

    windows下MYSQL自动备份批处理 2011-05-04 09:16:45|  分类: mysql|举报|字号 订阅     按系统时间进行备份 注意mysql安装路径中如果有空格.就要把,.b ...

  7. IP地址规划和设计方法

    IP地址规划和设计方法 无类域间路由技术需要在提高 IP 地址利用率和减少主干路由器负荷两个方面取得平衡 网络地址转换 NAT 最主要的应用是专用网,虚拟专用网,以及 ISP 为拨号用户 提供的服务 ...

  8. 领域驱动设计(DDD)

    领域驱动设计(DDD)实现之路 2004年,当Eric Evans的那本<领域驱动设计——软件核心复杂性应对之道>(后文简称<领域驱动设计>)出版时,我还在念高中,接触到领域驱 ...

  9. 【Oracle】-【插入读取顺序】-插入读取之间的顺序关系

    Oracle插入记录的顺序是否是读取的顺序? 通过一个简单的实验验证: SQL> create table t ( x int, a char(2000) default 'x', b char ...

  10. windows 8以上找回开始菜单

    步骤如下: 右击任务栏,选择工具栏——新建工具 在工具栏---新建工具栏的输入框中输入,”C:\ProgramData\Microsoft\Windows\Start Menu\Programs,然后 ...