Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Magento Extension @ PHPConf Taiwan 2013

kiang
October 05, 2013

Magento Extension @ PHPConf Taiwan 2013

Give a hello world example to explain how to develop extensions in Magento

kiang

October 05, 2013
Tweet

More Decks by kiang

Other Decks in Technology

Transcript

  1. by kiang @ phpconf taiwan 2013 Magento Extension

  2. Who is kiang? • Google “Finjon Kiang” → About 85,100

    results (0.27 seconds) @ 00:42 today • Taiwan PHP User Group → http://twpug.net • Just This Computer Studio → http://olc.tw • Just Play → http://travel.olc.tw
  3. Why English? Because my English is poor. I need more

    practices, Sorry for those who hate English.
  4. Why Magento? • 169,571 live websites using Magento today →

    http://trends.builtwith.com/shop/Magento • Magento ran on 11% of the top 10K sites → http://trends.builtwith.com/shop • Some customers said “Hey, we want to use Magento” ...This is a real life
  5. <?php echo 'hello world';

  6. <?php echo 'hello world'; app/etc/modules/Olctw_Phpconf.xml <?xml version="1.0"?> <config> <modules> <Olctw_Phpconf>

    <active>true</active> <codePool>community</codePool> </Olctw_Phpconf> </modules> </config>
  7. <?php echo 'hello world'; app/code/ → core → community →

    local
  8. <?php echo 'hello world'; app/code/community/Olctw/Phpconf/ → etc/config.xml → controllers/IndexController.php

  9. <?php echo 'hello world'; etc/config.xml <?xml version="1.0" encoding="UTF-8"?> <config> <frontend>

    <routers> <phpconf> <use>standard</use> <args> <module>Olctw_Phpconf</module> <frontName>phpconf</frontName> </args> </phpconf> </routers> </frontend> </config>
  10. <?php echo 'hello world'; controllers/IndexController.php <?php class Olctw_Phpconf_IndexController extends Mage_Core_Controller_Front_Action

    { public function indexAction() { echo 'hello world'; } }
  11. <?php echo 'hello world';

  12. <?php echo 'hello world'; controllers/IndexController.php $this->loadLayout(); $block = $this->getLayout()->createBlock('core/text')->setTex t('hello

    world'); $this->getLayout()->getBlock('content')->append($ block); $this->renderLayout();
  13. <?php echo 'hello world';

  14. <?php echo 'hello world'; Move 'hello world' from controller to

    block
  15. <?php echo 'hello world'; etc/config.xml <config> <global> <blocks> <Olctw_Phpconf> <class>Olctw_Phpconf_Block</class>

    </Olctw_Phpconf> </blocks> </global> </config>
  16. <?php echo 'hello world'; app/code/community/Olctw/Phpconf/ → Block/Hello.php

  17. <?php echo 'hello world'; Block/Hello.php <?php class Olctw_Phpconf_Block_Hello extends Mage_Core_Block_Template

    { protected function _toHtml() { return 'hello world'; } }
  18. <?php echo 'hello world'; controllers/IndexController.php $block = $this->getLayout()->createBlock('core/text')->setTex t('hello world');

    → $block = $this->getLayout()->createBlock('Olctw_Phpconf/hell o');
  19. <?php echo 'hello world'; put 'hello world' block into layout

  20. <?php echo 'hello world'; controllers/IndexController.php public function indexAction() { $this->loadLayout();

    $this->renderLayout(); }
  21. <?php echo 'hello world'; etc/config.xml <config> <frontend> <layout> <updates> <Olctw_Phpconf>

    <file>olctw_phpconf.xml</file> </Olctw_Phpconf> </updates> </layout> </frontend> </config>
  22. <?php echo 'hello world'; app/design/frontend/base/default/layout/ → olctw_phpconf.xml

  23. <?php echo 'hello world'; olctw_phpconf.xml <?xml version="1.0"?> <layout version="0.1.0"> <phpconf_index_index>

    <reference name="content"> <block type="Olctw_Phpconf/hello"></block> </reference> </phpconf_index_index> </layout>
  24. <?php echo 'hello world'; move 'hello world' to template

  25. <?php echo 'hello world'; olctw_phpconf.xml <block type="Olctw_Phpconf/hello" template="olctw_phpconf/hello.phtml"></block>

  26. <?php echo 'hello world'; app/design/frontend/base/default/template/ → olctw_phpconf/hello.phtml

  27. <?php echo 'hello world'; olctw_phpconf/hello.phtml hello world

  28. <?php echo 'hello world'; Block/Hello.php <?php class Olctw_Phpconf_Block_Hello extends Mage_Core_Block_Template

    { }
  29. <?php echo 'hello world'; Interaction between block and template

  30. <?php echo 'hello world'; Block/Hello.php public function getStoreName() { return

    Mage::app()->getStore()->getName(); }
  31. <?php echo 'hello world'; olctw_phpconf/hello.phtml hello world @ <?php echo

    $this->getStoreName(); ? >
  32. <?php echo 'hello world';