+ All Categories
Home > Spiritual > How to Create A Magento Adminhtml Controller in Magento Extension

How to Create A Magento Adminhtml Controller in Magento Extension

Date post: 19-Aug-2015
Category:
Upload: hendy-irawan
View: 18,726 times
Download: 3 times
Share this document with a friend
Popular Tags:
25
How to Create Adminhtml Controller in Magento Extension Creating your own Adminhtml pages in Magento. Hendy Irawan at Bippo Indonesia
Transcript

How to Create Adminhtml Controller in Magento Extension

Creating your own Adminhtml pages in Magento.

Hendy Irawan at Bippo Indonesia

URL Structure

http://demo.bippo.co.id   /admin/module/controller/action/key/***

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_Adminhtml_CreativesController extends Mage_Adminhtml_Controller_Action

{

public function helloAction(){

}

}

Bippo/MyBanner/controllers/Adminhtml/CreativesController.php

Router Config

...<admin> <routers> <mybanner> <use>admin</use> <args> <module>Bippo_MyBanner</module> <frontName>mybanner</frontName> </args> </mybanner> </routers> </admin>...

Bippo/MyBanner/etc/config.xml

"Raw" Action

Simple Action

public function helloAction(){

    echo "Hello World";

}

Admin Panel Menu Link

<adminhtml> <menu> <catalog> <children> <mybanner_admincreatives translate="title" module="mybanner"> <title>Banner Creatives</title> <action>mybanner/adminhtml_creatives</action> </mybanner_admincreatives> </children> </catalog> </menu></adminhtml>

Bippo/MyBanner/etc/config.xml

Test it

Go to Admin Panel Navigate to "Catalog > Banner Creatives"

Layout-based Action

Layout Config

<adminhtml> <layout> <updates> <mybanner> <file>mybanner.xml</file> </mybanner> </updates> </layout> </adminhtml>

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 Adminhtml Action URLfrom PHP

$url = Mage::helper('adminhtml') ->getUrl('mybanner/adminhtml_creatives/edit', array('city'=>'Bandung') );

GET & Post Variables

$request = Mage::app()->getRequest();

// GET$productId = $request->getParam('product_id', 44);// 44 = default value if "product_id" not specified

// POST, method 1$description = $request->getPost('description', "hi");

// POST, method 2$postData = $request->getPost();$description = $postData['description'];

Process Input

<?php$url = Mage::helper('adminhtml')->getUrl('mybanner/adminhtml_creatives/edit');<form action="<?php echo $url ?>" method="post"> <?php echo $this->getBlockHtml('formkey')?> <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();}

Get & Set Session Variables

$session = Mage::getSingleton('core/session');

// get$token = $session->getAccessToken();

// set$session->setAddress('Rereongan Sarupi');

Support Messages Block

$this­>_initLayoutMessages('adminhtml/session');

Other choices: customer/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://www.magentocommerce.com/wiki/5_­_modules_and_development/0_­_module_development_in_magento/how_to_create_an_admin_form_module

http://www.magentocommerce.com/boards/viewthread/19386/

http://inchoo.net/ecommerce/magento/getting­started­with­building­admin­module­in­magento/


Recommended