magento开发-- 新建你自己的controller
发布时间:2014-12-22 09:54:29
发布时间:2014-12-22 09:54:29
magento开发 -- 新建你自己的controller 收藏
Magento is based on MVC model. This model helps for defining models, view (layout + templates) and controllers. Despite big amount of modules available by default in Magento and on Magento Connect, you may want to create your own module and define your controller for you Magento website. 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.
view plaincopy to clipboardprint?
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
view plaincopy to clipboardprint?
1. version="1.0" encoding="UTF-8"?>
2.
3.
4.
5.
6.
7.
8.
And a file app/etc/modules/Baobaz_Arithmetic.xml for its activation:
view plaincopy to clipboardprint?
1. version="1.0" encoding="UTF-8"?>
2.
3.
4.
5.
6.
7.
8.
9.
For more informations about creation of a new extension, please check Wojtek's post "Developing module for Magento Tutorial - Where to Begin [Part 1]".
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:
view plaincopy to clipboardprint?
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:
view plaincopy to clipboardprint?
1.
2. ...
3.
4.
5.
6. standard
7.
8.
9.
10.
11.
12.
13.
14.
Let see how this router declaration works:
∙
∙
∙
∙ can take value standard (for front part) or admin (for admin part).
∙
∙
We can now modify multiplyAction method for making it displaying a message:
view plaincopy to clipboardprint?
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:
view plaincopy to clipboardprint?
1.
2. ...
3.
4. ...
5.
6.
7.
8.
9.
10.
11.
12.
13.
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.
view plaincopy to clipboardprint?
1. version="1.0" encoding="UTF-8"?>
2.
3.
4.
5.
6. page/1column.phtml
7.
8.
9.
10.
11.
12.
13.
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
view plaincopy to clipboardprint?
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
view plaincopy to clipboardprint?
1.
2.
3.
4.
5.
6. type="text" id="int1" name="int1" />
7.
8.
9.
10. type="text" id="int2" name="int2" />
11.
12.
13.
14.
15.
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.
view plaincopy to clipboardprint?
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:
view plaincopy to clipboardprint?
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:
view plaincopy to clipboardprint?
1. $this->_initLayoutMessages('customer/session');
Add then in you template the following line where you want to display the result
view plaincopy to clipboardprint?
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 baobaz_arithmetic.tar.gz archive.