Upload
hendy-irawan
View
15.094
Download
2
Embed Size (px)
DESCRIPTION
Citation preview
How to Create A Controller in Magento Extension
Bippo Commerce
Creating your own pages in Magento.
URL Structure
http://demo.bippo.co.id/module/controller/action
Create Module's etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Bippo_MyBanner>
<version>1.0.0</version>
</Bippo_MyBanner>
</modules>
</config>
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>
Controller Class PHP Code
class Bippo_MyBanner_DisplayController extends Mage_Core_Controller_Front_Action
{
public function showAction(){
}
}
Bippo/MyBanner/controllers/DisplayController.php
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
"Raw" Action
Test
public function helloAction(){
echo "Hello World";
}
Test it
http://localhost/magento/mybanner/display/hello
Layout-based Action
Layout Config
<frontend> <layout> <updates> <mybanner> <file>mybanner.xml</file> </mybanner> </updates> </layout> </frontend>
Bippo/MyBanner/etc/config.xml
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
Load Layout in Action
public function smallAction(){
$this->loadLayout();
$this->renderLayout();
}
Test
/mybanner/display/small
Get URL from PHP
<?php echo Mage::getUrl('mybanner/display/small') ?>
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>
Is Form POST ?
$this>getRequest()>isPost()
Process POST Parameters
public 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();}
Support Messages Block
$this>_initLayoutMessages('customer/session');
Other choices: adminhtml/session catalog/session checkout/session
Add Message
Mage::getSingleton('customer/session')>addSuccess("$int1 * $int2 = $result");
Display Message in PHTML Template
echo $this>getMessagesBlock()>getGroupedHtml();
References
http://blog.baobaz.com/en/blog/magentomodulecreateyourowncontroller