以helloworld为例讲解magento中控制器的工作,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:
以上解释一下: 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
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
先找全局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官方文档。 (责任编辑:好模板) |