Magento : Créer un contrôleur dans l’admin sous un nouvel onglet

Nous avons déjà vu dans un précédent billet comment surcharger un contrôleur dans Magento. Cette fois, je vais vous montrer comment créer votre propre contrôleur dans l’admin et cela proprement, car il y a beaucoup de solutions proposées ici et là mais elles ne fonctionnent pas toutes et sont parfois obsolètes. Nous accéderons à notre contrôleur depuis un nouvel onglet que nous allons là aussi définir nous-même.

Aperçu

Avant de rentrer dans le code, voici à quoi va ressembler l’admin Magento :

Et voici le contenu de notre module :

Configuration du module

app/code/community/JR/CreateAdminController/etc/config.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <JR_CreateAdminController>
            <version>1.0.0</version>
        </JR_CreateAdminController>
    </modules>
    <global>
        <helpers>
            <jr_createadmincontroller>
                <!-- Helper definition needed by Magento -->
                <class>Mage_Core_Helper</class>
            </jr_createadmincontroller>
        </helpers>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <foo_bar before="Mage_Adminhtml">JR_CreateAdminController_Adminhtml</foo_bar>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Création de l’onglet et des sous-onglets

app/code/community/JR/CreateAdminController/etc/adminhtml.xml :

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <menu>
        <mycustomtab module="jr_createadmincontroller" translate="title">
            <title>My Custom Tab</title>
            <sort_order>100</sort_order>
            <children>
                <index module="jr_createadmincontroller" translate="title">
                    <title>Index Action</title>
                    <sort_order>1</sort_order>
                    <action>adminhtml/custom</action>
                </index>
                <list module="jr_createadmincontroller" translate="title">
                    <title>List Action</title>
                    <sort_order>2</sort_order>
                    <action>adminhtml/custom/list</action>
                </list>
            </children>
        </mycustomtab>
    </menu>
    <acl>
        <resources>
            <admin>
                <children>
                    <custom translate="title" module="jr_createadmincontroller">
                        <title>My Controller</title>
                        <sort_order>-100</sort_order>
                        <children>
                            <index translate="title">
                                <title>Index Action</title>
                                <sort_order>1</sort_order>
                            </index>
                            <list translate="title">
                                <title>List Action</title>
                                <sort_order>2</sort_order>
                            </list>
                        </children>
                    </custom>
                </children>
            </admin>
        </resources>
    </acl>
</config>

Création du contrôleur

app/code/community/JR/CreateAdminController/controllers/Adminhtml/CustomController.php :

<?php
 
class JR_CreateAdminController_Adminhtml_CustomController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('mycustomtab')
            ->_title($this->__('Index Action'));
 
        // my stuff
 
        $this->renderLayout();
    }
 
    public function listAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('mycustomtab')
            ->_title($this->__('List Action'));
 
        // my stuff
 
        $this->renderLayout();
    }
}

Téléchargement

Le code de l’extension est disponible sur GitHub : https://github.com/jreinke/magento-create-admin-controller-example/downloads. A vous de jouer maintenant ! ;)

32 réponses à “Magento : Créer un contrôleur dans l’admin sous un nouvel onglet”

  1. Ben Marks dit :

    Might be good to point out that users will need to create the data helper class based on the translate arguments

    JR_CreateAdminController_Helper_Data extends Mage_Core_Helper_Data {}

  2. Rosario dit :

    where’s the code of this extension ? thank you

  3. rohit dit :

    how can i put my file path URL in this module please Suggest me …..

  4. Bharath dit :

    Hey Rohit,

    Found any solution? I’m held up with the same.

  5. Wrong Answer dit :

    Have Same Problem … Fatel error for helper_data.. though i make it..

  6. Afsana dit :

    Nice Code…. But its not working….. :'( Please help me for this…..

  7. Sven Varkel dit :

    Thanks for tutorial. I needed a little piece from there to remind me how adminhtml controllers are mapped:)

  8. asd dit :

    LOL this one is also not working

  9. Rajib dit :

    How to make sub->sub menu using this code. Please explain.

  10. Hélio dit :

    this was very helpful your post, but you have the part of View?
    How do I list items from a database table, for example?

  11. andrei dit :

    it doesn’t work…..

  12. andrei dit :

    no, wait… sorry. It works.. I just needed to clear the catch. Thanks

  13. shilpa dit :

    pls suggest …. its not working

  14. shilpa dit :

    hi
    i followed the steps given above many times….but still not showing any menu item/tab in magento admin panel’s menu bar………plz help

  15. shilpa dit :

    hi
    plz tell me how to give links on sub-tab…..

    • Mjx dit :

      For Example:
      your Extension is in location: Mjx/Shipping
      Mjx is your Namespace and Shipping is your extension name
      and you have Controller : TestController.php in location Mjx/Shipping/controller/Adminhtml/
      and this controller will have method
      public function listAction() { die(‘test’) }

      So your main tab hould have

      List

      adminhtml/shipping/list

      … <– In this param you create link The value is adminhtml(always)/shipping(this is the name of router you defined in section)/list(this is action from controller)

      I hope this helped you

  16. Vikram dit :

    I have tried your extension and it worked well , I have found one issue with the extension.

    If i set custom ROLE then other user with roles/permission not able to view this menu in his account. For admin its working fine , but any secondary user have permission to this module will not able to view it in his account.

    Any idea whats wrong ?

  17. Vikram dit :

    must be

    only then ACl will work

  18. david dit :

    It works fine and displayed tab in backend. how to add form fields in backend

  19. sreenu dit :

    This tutorial very helpful thank you it’s working.
    I created menu and submenu
    in that i want to groups and fields(item)
    i tried but not getting
    please help me
    thank you

  20. bratati dit :

    If I want to rewrite this controller JR_CreateAdminController_Adminhtml_CustomController by another controller, how can I achieve that.

  21. arjun dit :

    That is my error please solve it or give me sugestion..

    Warning: simplexml_load_string(): Entity: line 10: parser error : Extra content at the end of the document in C:\xampp\htdocs\magento\lib\Varien\Simplexml\Config.php on line 510

    #0 [internal function]: mageCoreErrorHandler(2, ‘simplexml_load_…’, ‘C:\\xampp\\htdocs…’, 510, Array)
    #1 C:\xampp\htdocs\magento\lib\Varien\Simplexml\Config.php(510): simplexml_load_string(‘loadString(‘loadFile(‘C:\\xampp\\htdocs…’)
    #4 C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Config.php(315): Mage_Core_Model_Config->_loadDeclaredModules()
    #5 C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\App.php(414): Mage_Core_Model_Config->loadModules()
    #6 C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\App.php(343): Mage_Core_Model_App->_initModules()
    #7 C:\xampp\htdocs\magento\app\Mage.php(684): Mage_Core_Model_App->run(Array)
    #8 C:\xampp\htdocs\magento\index.php(89): Mage::run( », ‘store’)
    #9 {main}

  22. vipin kumar dit :

    hello,
    your module is working and i have installed it and tab created in admin section but i want to show table record when click on the tab so please help me how to create listing of the records. my magento version is 1.9.1.0

  23. Mukesh dit :

    How, I can create admin view?

  24. Naveen dit :

    I have done an basic extension setup with this tutorial, i have assigned a admin user group and gave permission to the module, but when i login with that user group i am getting error message like « Access denied
    Please try to logout and sign in again.
    If this message persists, please contact the store owner. » is there anything i have missed ? to give in the adminhtml controller?

  25. Purusottama dit :

    Hi, Its work for me thanks.
    I want some list with minimum functionality. list pagination search etc.
    This only show blank page in admin site (body part empty) hope you get me.

    thanks
    puru

  26. Gopal dit :

    Nice one,
    I used this for Help Document in Magento but i cant give link to PDF (which is placed inside my server) or to give link to external server.

    How to do it. Please post them here.

    Thanks.

  27. kumar harsh dit :

    how to change image appearence of home page on Index Action click event
    please help me

  28. vijay dit :

    please help this code is not working

  29. Julian Mancera dit :

    TANK YOU VERY MUCH , i cant call different funtions of a controller, i didn know the sintax, it was a simply « _ » after methot ,adminhtml/custom/list , for this I can call a different layout for each button whitout usign more tahn one controler and layout

  30. Alex dit :

    Great Tutorial! Quick question, how do I change the url from « admin/custom/index », to read « admin/foobar/index »
    Thanks again,
    Alex

Laisser un commentaire

* Champs requis

Categories