Magentois based onMVCmodel. This model helps for defining models, view (layout + templates) andcontrollers. Despite big amount of modules available by default in Magento and onMagento Connect, you may want to create your own module and define yourcontrollerfor youMagento website. No problem, this tutorial will explain you how to create your owncontrollerand how to make it respect its authoritah tolayoutsandtemplates.
Purpose of this examplecontrollerwill 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 namedarithmetic. Folders needed for this extension are created.
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->$mkdir-papp/code/local/Baobaz/Arithmetic/controllers
<!--[if !supportLists]-->2. <!--[endif]-->$mkdir-papp/code/local/Baobaz/Arithmetic/etc
We create fileapp/code/local/Baobaz/Arithmetic/etc/config.xml, in order to register this extension
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]--><?xmlversion="1.0"encoding="UTF-8"?>
<!--[if !supportLists]-->2. <!--[endif]--><config>
<!--[if !supportLists]-->3. <!--[endif]--><modules>
<!--[if !supportLists]-->4. <!--[endif]--><baobaz_arithmetic>
<!--[if !supportLists]-->5. <!--[endif]--><version>0.0.1</version>
<!--[if !supportLists]-->6. <!--[endif]--></baobaz_arithmetic>
<!--[if !supportLists]-->7. <!--[endif]--></modules>
<!--[if !supportLists]-->8. <!--[endif]--></config>
And a fileapp/etc/modules/Baobaz_Arithmetic.xmlfor its activation:
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]--><?xmlversion="1.0"encoding="UTF-8"?>
<!--[if !supportLists]-->2. <!--[endif]--><config>
<!--[if !supportLists]-->3. <!--[endif]--><modules>
<!--[if !supportLists]-->4. <!--[endif]--><Baobaz_Arithmetic>
<!--[if !supportLists]-->5. <!--[endif]--><active>true</active>
<!--[if !supportLists]-->6. <!--[endif]--><codePool>local</codePool>
<!--[if !supportLists]-->7. <!--[endif]--></Baobaz_Arithmetic>
<!--[if !supportLists]-->8. <!--[endif]--></modules>
<!--[if !supportLists]-->9. <!--[endif]--></config>
For more informations about creation of a new extension, please checkWojtek's post"Developing module for Magento Tutorial - Where to Begin [Part 1]".
Creating acontroller
You need now to create fileapp/code/local/Baobaz/Arithmetic/controllers/IntegerController.phpand write method that will be used for multiplication.
Controllerfiles must always follow patternxxxxxController.php(xxxxxwill be used after in url for calling thiscontroller) and put incontrollersfolder.
Controllersmethods names must follow patternyyyyyAction(yyyyywill also be used in url for calling thiscontroller). For the moment content of our file is:
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->classBaobaz_Arithmetic_IntegerControllerextendsMage_Core_Controller_Front_Action
<!--[if !supportLists]-->2. <!--[endif]-->{
<!--[if !supportLists]-->3. <!--[endif]-->publicfunctionmultiplyAction(){
<!--[if !supportLists]-->4. <!--[endif]-->}
<!--[if !supportLists]-->5. <!--[endif]-->}
We need to indicate now that somecontrollersare available inArithmeticmodules. For doing that, we add the following content inapp/code/local/Baobaz/Arithmetic/etc/config.xmlfile:
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]--><config>
<!--[if !supportLists]-->2. <!--[endif]-->...
<!--[if !supportLists]-->3. <!--[endif]--><frontend>
<!--[if !supportLists]-->4. <!--[endif]--><routers>
<!--[if !supportLists]-->5. <!--[endif]--><arithmetic>
<!--[if !supportLists]-->6. <!--[endif]--><use>standard</use>
<!--[if !supportLists]-->7. <!--[endif]--><args>
<!--[if !supportLists]-->8. <!--[endif]--><module>Baobaz_Arithmetic</module>
<!--[if !supportLists]-->9. <!--[endif]--><frontName>arithmetic</frontName>
<!--[if !supportLists]-->10. <!--[endif]--></args>
<!--[if !supportLists]-->11. <!--[endif]--></arithmetic>
<!--[if !supportLists]-->12. <!--[endif]--></routers>
<!--[if !supportLists]-->13. <!--[endif]--></frontend>
<!--[if !supportLists]-->14. <!--[endif]--></config>
Let see how this router declaration works:
<!--[if !supportLists]-->· <!--[endif]--><frontend>indicates that router will be use in front part of website
<!--[if !supportLists]-->· <!--[endif]--><routers>is where you declare all your routers
<!--[if !supportLists]-->· <!--[endif]--><arithmetic>is identifier of this router
<!--[if !supportLists]-->· <!--[endif]--><use>standard</use>can take valuestandard(for front part) oradmin(for admin part).
<!--[if !supportLists]-->· <!--[endif]--><module>Baobaz_Arithmetic</module>indicates which module contain controller that handles this router
<!--[if !supportLists]-->· <!--[endif]--><frontName>arithmetic</frontName>is router name that will be used in url
We can now modifymultiplyActionmethod for making it displaying a message:
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->publicfunctionmultiplyAction(){
<!--[if !supportLists]-->2. <!--[endif]-->echo"Respectmyauthoritah";
<!--[if !supportLists]-->3. <!--[endif]-->}
When you call now urlhttp://monsitemagento/arithmetic/integer/multiplymessage"Respect my authoritah"will be displayed. Let dissect this url:
<!--[if !supportLists]-->· <!--[endif]-->arithmetictells thatcontrolleris inBaobaz_Arithmeticmodule
<!--[if !supportLists]-->· <!--[endif]-->integertells thatcontrollers/integerController.phpfile must be cehcked
<!--[if !supportLists]-->· <!--[endif]-->multiplytells thatmultiplyActionmethod must be chosen in this file
Displaying a template
We define which layout file will be used in the module:
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]--><config>
<!--[if !supportLists]-->2. <!--[endif]-->...
<!--[if !supportLists]-->3. <!--[endif]--><frontend>
<!--[if !supportLists]-->4. <!--[endif]-->...
<!--[if !supportLists]-->5. <!--[endif]--><layout>
<!--[if !supportLists]-->6. <!--[endif]--><updates>
<!--[if !supportLists]-->7. <!--[endif]--><arithmetic>
<!--[if !supportLists]-->8. <!--[endif]--><file>arithmetic.xml</file>
<!--[if !supportLists]-->9. <!--[endif]--></arithmetic>
<!--[if !supportLists]-->10. <!--[endif]--></updates>
<!--[if !supportLists]-->11. <!--[endif]--></layout>
<!--[if !supportLists]-->12. <!--[endif]--></frontend>
<!--[if !supportLists]-->13. <!--[endif]--></config>
We createapp/design/frontend/default/default/layout/arithmetic.xmlfile in order to define which blocks will be used forcontrollerthat was just made.
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]--><?xmlversion="1.0"encoding="UTF-8"?>
<!--[if !supportLists]-->2. <!--[endif]--><layoutversion="0.1.0">
<!--[if !supportLists]-->3. <!--[endif]--><arithmetic_integer_multiply>
<!--[if !supportLists]-->4. <!--[endif]--><referencename="root">
<!--[if !supportLists]-->5. <!--[endif]--><actionmethod="setTemplate">
<!--[if !supportLists]-->6. <!--[endif]--><template>page/1column.phtml</template>
<!--[if !supportLists]-->7. <!--[endif]--></action>
<!--[if !supportLists]-->8. <!--[endif]--></reference>
<!--[if !supportLists]-->9. <!--[endif]--><referencename="content">
<!--[if !supportLists]-->10. <!--[endif]--><blocktype="core/template"name="arithmetic_integer_multiply"template="arithmetic/integer/multiply.phtml"></block>
<!--[if !supportLists]-->11. <!--[endif]--></reference>
<!--[if !supportLists]-->12. <!--[endif]--></arithmetic_integer_multiply>
<!--[if !supportLists]-->13. <!--[endif]--></layout>
Main template used byarithmetic/integer/multiplyispage/1column.phtml. For"content"part of this template, onlyarithmetic_integer_multiplyblock will be displayed. This block does not need any particular management. It is then set withcore/templatetype that is default type. Template file used will bearithmetic/integer/multiply.phtml.
Ourtemplateis defined,app/design/frontend/default/default/template/arithmetic/integer/multiply.phtmlmust then be created. This file will be empty for the moment..
For displaying correctly layout, it must be loaded incontroller
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->publicfunctionmultiplyAction(){
<!--[if !supportLists]-->2. <!--[endif]-->$this->loadLayout();
<!--[if !supportLists]-->3. <!--[endif]-->$this->renderLayout();
<!--[if !supportLists]-->4. <!--[endif]-->}
Interaction between template and controller
Our template will just have a basic form for providing integers to multiply
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]--><formaction="<?phpechoMage::getUrl('arithmetic/integer/multiply')?>"method="post">
<!--[if !supportLists]-->2. <!--[endif]--><fieldset>
<!--[if !supportLists]-->3. <!--[endif]--><ul>
<!--[if !supportLists]-->4. <!--[endif]--><li>
<!--[if !supportLists]-->5. <!--[endif]--><labelfor="int1">Integer1</label>
<!--[if !supportLists]-->6. <!--[endif]--><inputtype="text"id="int1"name="int1"/>
<!--[if !supportLists]-->7. <!--[endif]--></li>
<!--[if !supportLists]-->8. <!--[endif]--><li>
<!--[if !supportLists]-->9. <!--[endif]--><labelfor="int2">Integer2</label>
<!--[if !supportLists]-->10. <!--[endif]--><inputtype="text"id="int2"name="int2"/>
<!--[if !supportLists]-->11. <!--[endif]--></li>
<!--[if !supportLists]-->12. <!--[endif]--><li><inputtype="submit"value="Multiply"/></li>
<!--[if !supportLists]-->13. <!--[endif]--></ul>
<!--[if !supportLists]-->14. <!--[endif]--></fieldset>
<!--[if !supportLists]-->15. <!--[endif]--></form>
Action form url is againarithmetic/integer/multiply.Controlleraction must then be modified in order to manage data from form and to give result.
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->publicfunctionmultiplyAction(){
<!--[if !supportLists]-->2. <!--[endif]-->if($this->getRequest()->isPost()){
<!--[if !supportLists]-->3. <!--[endif]-->$int1=$this->getRequest()->getPost('int1');
<!--[if !supportLists]-->4. <!--[endif]-->$int2=$this->getRequest()->getPost('int2');
<!--[if !supportLists]-->5. <!--[endif]-->$result=$int1*$int2;
<!--[if !supportLists]-->6. <!--[endif]-->Mage::getSingleton('customer/session')->addSuccess("$int1*$int2=$result");
<!--[if !supportLists]-->7. <!--[endif]-->}
<!--[if !supportLists]-->8. <!--[endif]-->$this->loadLayout();
<!--[if !supportLists]-->9. <!--[endif]-->$this->_initLayoutMessages('customer/session');
<!--[if !supportLists]-->10. <!--[endif]-->$this->renderLayout();
<!--[if !supportLists]-->11. <!--[endif]-->}
In order to know ifcontrolleris called after using form, following instruction is used:
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->$this->getRequest()->isPost()
Result is put in'customer/session'session. For being able to display this result in template, message template must be loaded inmultiplyActionmethod:
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->$this->_initLayoutMessages('customer/session');
Add then in you template the following line where you want to display the result
view plaincopy to clipboardprint?
<!--[if !supportLists]-->1. <!--[endif]-->echo$this->getMessagesBlock()->getGroupedHtml();
And here we are: we have now a newcontrollerthat displays result of a multiplication.
All files used in this tutorial are available inbaobaz_arithmetic.tar.gzarchive.