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”
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 {}
where’s the code of this extension ? thank you
how can i put my file path URL in this module please Suggest me …..
Hey Rohit,
Found any solution? I’m held up with the same.
Have Same Problem … Fatel error for helper_data.. though i make it..
Nice Code…. But its not working….. :'( Please help me for this…..
Thanks for tutorial. I needed a little piece from there to remind me how adminhtml controllers are mapped:)
LOL this one is also not working
How to make sub->sub menu using this code. Please explain.
this was very helpful your post, but you have the part of View?
How do I list items from a database table, for example?
it doesn’t work…..
no, wait… sorry. It works.. I just needed to clear the catch. Thanks
pls suggest …. its not working
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
hi
plz tell me how to give links on sub-tab…..
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
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 ?
must be
only then ACl will work
It works fine and displayed tab in backend. how to add form fields in backend
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
If I want to rewrite this controller JR_CreateAdminController_Adminhtml_CustomController by another controller, how can I achieve that.
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}
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
How, I can create admin view?
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?
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
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.
how to change image appearence of home page on Index Action click event
please help me
please help this code is not working
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
Great Tutorial! Quick question, how do I change the url from « admin/custom/index », to read « admin/foobar/index »
Thanks again,
Alex
great tutorial