Say Hello to October and welcome back to Magento Tutorial by Magestore!

Let’s continue Lesson 6. In part 1, we have learned how to add a Magento menu in backend. Today in part 2, we will learn how to create a Magento controller action in backend.

Let’s go!

1. Declare a router in file config.xml

File  app\code\local\Magestore\Lesson06\etc\config.xml
Overall configuration:

<admin>
<routers>
<adminhtml>
<args>
<modules>
<[Namespace]_[Modulename]_[Adminhtml] > [Namespace]_[Modulename]_Adminhtml</[Namespace]_[Modulename]_[Adminhtml]>
</modules>
</args>
</adminhtml>
</routers>
</admin>

Example:

<admin>
<routers>
<lesson06admin>
<use>admin</use>
<args>
<module>Magestore_Lesson06</module>
<frontName>lesson06admin</frontName>
</args>
</lesson06admin>
</routers>
</admin>

–    <admin> and <routers> are the parent nodes. 1 can be either
•    <admin> : for admin routers
•     <frontend> :  for frontend routers
–    <lesson06admin>: The name of your module in lowercase. You can indicate a name that is already in use (for example adminthml) only if you want to extend the configuration of that route with a child entry in <modules> node (see below). Other settings will not be rewritten or may even cause errors.
–    <args> : node can contain three different types of arguments;
–    <module> : contains the full name of your module including package (Packagename_Modulename). The system will then look for controllers in the controllers directory of the module you indicated.
–    <frontName> : sets the router name as it will be included in the URL: /frontname/controller/action like /lesson06admin/ adminhtml_testbymagestore/index/key/69a015b1c7b4151122e352de306e1d0c/

2. Link structure in Magento

•    In backend:
Example : /lesson06admin/adminhtml_lesson06/view
in which:
–    lesson06admin: is the adminhtml router.
–    adminhtml_lesson06: is the controller name requested in backend (in folder adminhtml).
–    view: is the action in controller lesson06 requested.
•    In frontend:
Example: /lesson06/ lesson06/view
in which:
–    lesson06: is the frontend  router.
–    lesson06: is the controller requested in frontend.
–    view: is the action in the controller lesson06 requested.

3. Controller for each menu

•    Add controllers for each menu
Example
File app\code\local\Magestore\Lesson06\etc\adminhtml.xml

<?xml version="1.0"?>
<config>
<menu>
<lesson06 module="lesson06" translate="title">
<title>Lesson06</title>
<sort_order>71</sort_order>
<children>
<lesson06 module="lesson06" translate="title">
<title>Manage Items</title>
<sort_order>0</sort_order>
<action>
lesson06admin/adminhtml_lesson06
</action>
</lesson06>
. . .
</menu>
</config>

File controller : app\code\local\Magestore\Lesson06\controllers\Adminhtml\Lesson06Controller.php

<?php
class Magestore_Lesson06_Adminhtml_Lesson06Controller extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
$this->loadLayout()
->_setActiveMenu('lesson06/lesson06')
->_addBreadcrumb(
Mage::helper('adminhtml')->__('Items Manager'),
Mage::helper('adminhtml')->__('Item Manager')
);
return $this;
}

public function indexAction()
{
$this->_initAction()
->renderLayout();
}
}

Example 2:
File app\code\local\Magestore\Lesson06\etc\adminhtml.xml

<?xml version="1.0"?>
<config>
<menu>
<lesson06 module="lesson06" translate="title">
<title>Lesson06</title>
<sort_order>71</sort_order>
<children>
<lesson06 module="lesson06" translate="title">
<title>Test Action Menu</title>
<sort_order>1</sort_order>
<action>
lesson06admin/adminhtml_lesson06/test
</action>
</lesson06>

URL of action menu:

File controller : app\code\local\Magestore\Lesson06\controllers\Adminhtml\Lesson06Controller.php

<?php
class Magestore_Lesson06_Adminhtml_Lesson06Controller extends Mage_Adminhtml_Controller_Action
{
public function testAction(){
$block = $this->getLayout()-> createBlock('adminhtml/template')-> setTemplate('lesson06/test/rendertesttemplate.phtml');
echo $block->toHtml();
}
protected function _isAllowed()
{
return Mage::getSingleton('admin/session')->isAllowed('lesson06');
}

 

Here is the end of Lesson 6 – Create a Magento Controller action in backend of  Magento Open Course series. We will turn to Lesson 7 soon. Follow us in Magento Open Course for Magento Tutorial.

See you again.

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

1 Comment

Write A Comment