博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
magent controller
阅读量:4200 次
发布时间:2019-05-26

本文共 6845 字,大约阅读时间需要 22 分钟。

 is based on  model. This model helps for defining models, view (layout + templates) and controllers. Despite big amount of modules available by default in Magento and on , you may want to create your own module and define your controller for you . No problem, this tutorial will explain you how to create your own controller and how to make it respect its authoritah to layouts and templates.

Purpose of this example controller will be to give result of two integers multiplication (very useful if you lost your calculator). Integers will be provided through a basic form. Result will be displayed in a Magento notification.

Before starting creation of your module, please turn off the cache management in order to see immediately every change.

Creating your module

Our extension will be named arithmetic. Folders needed for this extension are created.

1.     $ mkdir -p app/code/local/Baobaz/Arithmetic/controllers  

2.     $ mkdir -p app/code/local/Baobaz/Arithmetic/etc  

 

We create file app/code/local/Baobaz/Arithmetic/etc/config.xml, in order to register this extension

1.     <?xml version="1.0" encoding="UTF-8"?>  

2.     <config>  

3.         <modules>  

4.             <baobaz_arithmetic>  

5.                 <version>0.0.1</version>  

6.             </baobaz_arithmetic>  

7.         </modules>  

8.     </config>  

 

And a file app/etc/modules/Baobaz_Arithmetic.xml for its activation:

1.     <?xml version="1.0" encoding="UTF-8"?>  

2.     <config>  

3.         <modules>  

4.             <Baobaz_Arithmetic>  

5.                 <active>true</active>  

6.                 <codePool>local</codePool>  

7.             </Baobaz_Arithmetic>  

8.         </modules>  

9.     </config>  

 

For more informations about creation of a new extension, please check 's post .

Creating a controller

You need now to create file app/code/local/Baobaz/Arithmetic/controllers/IntegerController.php and write method that will be used for multiplication.

Controller files must always follow pattern xxxxxController.php (xxxxx will be used after in url for calling this controller) and put in controllers folder. 

Controllers methods names must follow pattern yyyyyAction (yyyyy will also be used in url for calling this controller). For the moment content of our file is:

1.     class Baobaz_Arithmetic_IntegerController extends Mage_Core_Controller_Front_Action  

2.     {  

3.         public function multiplyAction(){  

4.         }  

5.     }  

 

We need to indicate now that some controllers are available in Arithmetic modules. For doing that, we add the following content in app/code/local/Baobaz/Arithmetic/etc/config.xml file:

1.     <config>  

2.         ...  

3.         <frontend>  

4.             <routers>  

5.                 <arithmetic>  

6.                     <use>standard</use>  

7.                     <args>  

8.                         <module>Baobaz_Arithmetic</module>  

9.                         <frontName>arithmetic</frontName>  

10.                  </args>  

11.              </arithmetic>  

12.          </routers>     

13.      </frontend>  

14.  </config>  

 

Let see how this router declaration works:

·                <frontend> indicates that router will be use in front part of website

·                <routers> is where you declare all your routers

·                <arithmetic> is identifier of this router

·                <use>standard</use> can take value standard (for front part) or admin (for admin part).

·                <module>Baobaz_Arithmetic</module> indicates which module contain controller that handles this router

·                <frontName>arithmetic</frontName> is router name that will be used in url

We can now modify multiplyAction method for making it displaying a message:

1.     public function multiplyAction(){  

2.         echo "Respect my authoritah";  

3.     }  

 

When you call now url http://monsitemagento/arithmetic/integer/multiply message "Respect my authoritah" will be displayed. Let dissect this url:

·                arithmetic tells that controller is in Baobaz_Arithmetic module

·                integer tells that controllers/integerController.php file must be cehcked

·                multiply tells that multiplyAction method must be chosen in this file

Displaying a template

We define which layout file will be used in the module:

1.     <config>  

2.         ...  

3.         <frontend>  

4.             ...  

5.             <layout>  

6.                 <updates>  

7.                     <arithmetic>  

8.                         <file>arithmetic.xml</file>  

9.                     </arithmetic>  

10.              </updates>  

11.          </layout>  

12.      </frontend>  

13.  </config>  

 

We create app/design/frontend/default/default/layout/arithmetic.xml file in order to define which blocks will be used for controller that was just made.

1.     <?xml version="1.0" encoding="UTF-8"?>  

2.     <layout version="0.1.0">  

3.         <arithmetic_integer_multiply>  

4.             <reference name="root">  

5.                 <action method="setTemplate">  

6.                     <template>page/1column.phtml</template>  

7.                 </action>  

8.             </reference>  

9.             <reference name="content">  

10.              <block type="core/template" name="arithmetic_integer_multiply" template="arithmetic/integer/multiply.phtml"></block>  

11.          </reference>  

12.      </arithmetic_integer_multiply>  

13.  </layout>  

 

Main template used by arithmetic/integer/multiply is page/1column.phtml. For "content" part of this template, only arithmetic_integer_multiply block will be displayed. This block does not need any particular management. It is then set with core/template type that is default type. Template file used will be arithmetic/integer/multiply.phtml.

Our template is defined, app/design/frontend/default/default/template/arithmetic/integer/multiply.phtml must then be created. This file will be empty for the moment..

For displaying correctly layout, it must be loaded in controller

1.     public function multiplyAction(){  

2.         $this->loadLayout();  

3.         $this->renderLayout();  

4.     }  

 

Interaction between template and controller

Our template will just have a basic form for providing integers to multiply

1.     <form action="<?php echo Mage::getUrl('arithmetic/integer/multiply') ?>" method="post">  

2.         <fieldset>  

3.             <ul>  

4.                 <li>  

5.                     <label for="int1">Integer 1</label>  

6.                     <input type="text" id="int1" name="int1" />  

7.                 </li>  

8.                 <li>  

9.                     <label for="int2">Integer 2</label>  

10.                  <input type="text" id="int2" name="int2" />  

11.              </li>  

12.              <li><input type="submit" value="Multiply" /></li>  

13.          </ul>  

14.      </fieldset>  

15.  </form>  

 

Action form url is again arithmetic/integer/multiply. Controller action must then be modified in order to manage data from form and to give result.

1.     public function multiplyAction(){  

2.         if ($this->getRequest()->isPost()){  

3.             $int1 = $this->getRequest()->getPost('int1');  

4.             $int2 = $this->getRequest()->getPost('int2');  

5.             $result = $int1 * $int2;  

6.         Mage::getSingleton('customer/session')->addSuccess("$int1 * $int2 = $result");  

7.         }  

8.         $this->loadLayout();  

9.         $this->_initLayoutMessages('customer/session');  

10.      $this->renderLayout();  

11.  }  

 

In order to know if controller is called after using form, following instruction is used:

1.     $this->getRequest()->isPost()  

 

Result is put in 'customer/session' session. For being able to display this result in template, message template must be loaded in multiplyAction method:

1.     $this->_initLayoutMessages('customer/session');  

 

Add then in you template the following line where you want to display the result

1.     echo $this->getMessagesBlock()->getGroupedHtml();  

 

And here we are: we have now a new controller that displays result of a multiplication.

All files used in this tutorial are available in  archive.

转载地址:http://wrdli.baihongyu.com/

你可能感兴趣的文章
使用QTP的.NET插件扩展技术测试ComponentOne的ToolBar控件
查看>>
用上帝之眼进行自动化测试
查看>>
为LoadRunner写一个lr_save_float函数
查看>>
PrefTest工作室全新力作-《性能测试与调优实战》课程视频即将上线
查看>>
质量度量分析与测试技术 培训大纲
查看>>
欢迎加入【亿能测试快讯】邮件列表!
查看>>
为什么我们的自动化测试“要”这么难
查看>>
LoadRunner性能脚本开发实战训练
查看>>
测试之途,前途?钱途?图何?
查看>>
测试设计与测试项目实战训练
查看>>
HP Sprinter:敏捷加速器
查看>>
单元测试培训PPT
查看>>
adb常用命令
查看>>
通过LR监控Linux服务器性能
查看>>
通过FTP服务的winsockes录制脚本
查看>>
LRwinsocket协议测试AAA服务器
查看>>
Net远程管理实验
查看>>
反病毒专家谈虚拟机技术 面临两大技术难题
查看>>
几种典型的反病毒技术:特征码技术、覆盖法技术等
查看>>
性能测试一般过程与LR性能测试过程
查看>>