Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How to create a magento controller in magento extension

19,337 views

Published on

Published in: Technology, News & Politics

How to create a magento controller in magento extension

  1. 1. How to Create A Controller in Magento Extension Bippo CommerceCreating your own pages in Magento.
  2. 2. URL Structurehttp://demo.bippo.co.id/module/controller/action
  3. 3. Create Modules etc/config.xml<?xml version="1.0" encoding="UTF-8"?><config> <modules> <Bippo_MyBanner > <version>1.0.0</version> </Bippo_MyBanner > </modules></config>
  4. 4. Activate in etc/modules/ {company}_{module}.xml<?xml version="1.0" encoding="UTF-8"?><config> <modules> <Bippo_MyBanner > <active>true</active> <codePool>community</codePool> </Bippo_MyBanner > </modules></config>
  5. 5. Controller Class PHP Codeclass Bippo_MyBanner_Display Controller extends Mage_Core_Controller_Front_Action{ public function showAction(){ }} Bippo/MyBanner/controllers/DisplayController.php
  6. 6. Router Config...<frontend> <routers> <mybanner> <use>standard</use> <args> <module>Bippo_MyBanner</module> <frontName>display</frontName> </args> </mybanner> </routers></frontend>... Bippo/MyBanner/etc/config.xml
  7. 7. "Raw" Action
  8. 8. Testpublic function helloAction(){    echo "Hello World";}
  9. 9. Test it http://localhost/magento/mybanner/display/hello
  10. 10. Layout-based Action
  11. 11. Layout Config<frontend> <layout> <updates> <mybanner> <file>mybanner.xml</file> </mybanner> </updates> </layout> </frontend> Bippo/MyBanner/etc/config.xml
  12. 12. Layout XML<?xml version="1.0" encoding="UTF-8"?> <layout version="0.1.0"> <arithmetic_integer_multiply> <reference name="root"> <action method="setTemplate"> <template>page/1column.phtml</template> </action> </reference> <reference name="content"> <block type="core/template" name="arithmetic_integer_multiply" template="arithmetic/integer/multiply.phtml"></block> </reference> </arithmetic_integer_multiply> </layout> bippomybanner.xml
  13. 13. Load Layout in Actionpublic function smallAction(){ $this->loadLayout(); $this->renderLayout();}
  14. 14. Test /mybanner/display/small
  15. 15. Get URL from PHP<?php echo Mage::getUrl(mybanner/display/small) ? >
  16. 16. Process Input<form action="<?php echo Mage::getUrl(arithmetic/integer/multiply) ?>" method="post"> <fieldset> <ul> <li> <label for="int1">Integer 1</label> <input type="text" id="int1" name="int1" /> </li> <li> <label for="int2">Integer 2</label> <input type="text" id="int2" name="int2" /> </li> <li><input type="submit" value="Multiply" /></li> </ul> </fieldset> </form>
  17. 17. Is Form POST ?$this­>getRequest()­>isPost()
  18. 18. Process POST Parameterspublic function multiplyAction(){ if ($this->getRequest()->isPost()){ $int1 = $this->getRequest()->getPost(int1); $int2 = $this->getRequest()->getPost(int2); $result = $int1 * $int2; Mage::getSingleton(customer/session) ->addSuccess("$int1 * $int2 = $result"); } $this->loadLayout(); $this->_initLayoutMessages(customer/session); $this->renderLayout();}
  19. 19. Support Messages Block$this­>_initLayoutMessages(customer/session);Other choices: adminhtml/session catalog/session checkout/session
  20. 20. Add Message    Mage::getSingleton(customer/session)­ >addSuccess("$int1 * $int2 = $result");
  21. 21. Display Message in PHTML Templateecho $this­>getMessagesBlock()­>getGroupedHtml();
  22. 22. References http://blog.baobaz.com/en/blog/magento­module­create­your­own­controller

×