This entry was posted on Tuesday, March 31st, 2009 at 4:09 am and is filed under development, howto. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Mar 31, 2009
Howto: Repackageable custom extension development in Magento – Part 2 – Admin Controller
Author: gaweee | Filed under: development, howtoCreate the administrative complement
Next we’ll create the administration controller and configuration to achieve the https://127.0.0.1/magento/index.php/twits/admin reservation. This took me a long time to figure out how to do nicely.
- We’ll begin by updating the adding 2 new files (controller and helper) for the purpose of the Hello world in the admin site
app/ etc/ modules/ - SavantDegrees_All.xml (Or what ever your company name might be) code/ local/ SavantDegrees/ (Or what ever your company name might be) Twits/ (Or whatever your module name might be) Block/ - HelloWorld.php controllers/ - AdminController.php - IndexController.php etc/ - config.xml Helper/ - Data.php - Then we obviously create the
adminController.php
We’ll make the admin page reuse the sameHello world blockthat we’ve created in Phase 1.<?php class SavantDegrees_Twits_AdminController extends Mage_Adminhtml_Controller_Action { public function indexAction() { $this->loadLayout() ->_addContent($this->getLayout()->createBlock('twits/helloWorld')) ->renderLayout(); } }
Note that this controller extends
Mage_Adminhtml_Controller_Actioninstead. This enforces security and loads the admin layout for this controller instead of the frontend version. With this done, you can now visit https://127.0.0.1/magento/index.php/twits/admin to see the page in action. Again, this produces our ubiquitous Hello world. You’d probably figured out by now that/twitsrefers to our frontend controller reservation by ourconfig.xmland then/adminrefers to theadminControllerthat we’ve just created. Now you can try https://127.0.0.1/magento/index.php/twits/admin/index to see theindexActionmethod being called. Try it with other action names! - Next we’ll do the
config.xmlmagic that allows us to use https://127.0.0.1/magento/index.php/admin/twits for the administration.<?xml version="1.0"?> <config> <modules> <SavantDegrees_Twits> <version>0.1.0</version> </SavantDegrees_Twits> </modules> <global> <helpers> <twits><class>SavantDegrees_Twits_Helper</class></twits> </helpers> <blocks> <twits> <class>SavantDegrees_Twits_Block</class> </twits> </blocks> </global> <adminhtml> <menu> <twits translate="title" module="twits"> <title>Twits</title> <sort_order>100</sort_order> <action>twits/admin</action> </twits> </menu> </adminhtml> <frontend> <routers> <SavantDegrees_Twits> <use>standard</use> <args> <module>SavantDegrees_Twits</module> <frontName>twits</frontName> </args> </SavantDegrees_Twits> </routers> </frontend> </config>
What’ changed?
- The
global > helper > [module]fragment defines the helper class that will help our future translation. We’ll be seeing the code for that shortly. - The
adminhtml > menu > [module]fragment allows us to create the menu to access the twits/admin page. This is vital for version 1.3 onwards as the menu generates the necessary url key to access the page.
- The
- And finally the helper
Data.phpfile<?php class SavantDegrees_Twits_Helper_Data extends Mage_Core_Helper_Abstract { }
Why bother? Its a quirk of Magento to try to translate the menu. So we NEED this helper. Otherwise Magento goes looking for a Mage_Twits_Helper_Data class. Which obviously doesnt exist! You could use the helper from existing Magento classes, but then you could use ALOT of stuff from Magento classes. And if you’re reading this tutorial, you’re obviously arent ready.
- When you’re done, go flush the cache by accessing
System > Cache Management > Flush Magento Cachein the admin system. Then check out the new Twit menu item in the administration menu bar!
» Moving along … Part 3 – Database
read users' comments (24)
24 Responses to “Howto: Repackageable custom extension development in Magento – Part 2 – Admin Controller”
Leave a Reply
Most Popular
- HOWTO: PHP and jQuery upload progress bar (48)
- JQuery Progress Bar 1.1 (42)
- Howto: Repackageable custom extension development in Magento - Part 2 - Admin Controller (24)
- Howto: Repackageable custom extension development in Magento - Part 8 - CRUD - Update (16)
- HOWTO: struts 2 i18n (13)
- JQuery Progress Bar 2.0 (12)
- JQuery Progress Bar 1.2 (11)
- Howto: Repackageable custom extension development in Magento (9)
- Howto: Repackageable custom extension development in Magento - Part 3 - Database (9)
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List (8)
Recent Comments
- learning methods: All the posts you talk
- best travel agency: Wow! I really enjoyed this
- Muzafar Ali: Hi, thanks for sharing a beautiful
- andy65007: After the progress bar
- Mark: Excellent tutorials. Most "how to
- Mark: As Jim said before, you
- Jeremy Roberts: I tried and it works
- Silas Nordes: Came to this site by
- 150cc mopeds: That was a superb blog
- Jason: if the situation is like
Latest Entries
- jQuery Progress Bar Configuration
- Extracting email addresses from inbox
- 10 Good (Free and Legal) Source for Photos and Images
- Howto: Backup Microsoft SQL Server Database, as in Dump it to a SQL Script (like MYSQL's sqldump)
- Managing client's expectation with wireframe software
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List
- JQuery Progress Bar 2.0
- HOWTO: Find icons for your new prototype system
- Google Maps Helper
- laying the cornerstones

December 17th, 2009 at 10:42 am
Wow great dude.. For the first time I’m looking at a Magento tutorial thats written for a beginner like me.. Thanks
July 31st, 2009 at 7:55 am
Hi gawee,
thanks so much for you quick reply and for giving me the idea what is possible.
I know better now what to look for and found this solution that I can use: http://activecodeline.com/entityextender-extend-all-magento-entity-objects-at-once/
I am not sure how often others are updating their store(s), if Magento puts out updates every 2 weeks it’s quite time demanding to each time properly test it.
Thanks again for this superb tutorial, things are so much clearer now!!!
July 30th, 2009 at 2:49 pm
Hey Kat
From what i understand you could extend the
Controllerand theModelthen edit the view.But thats reallyyyyyyyy troublesome. you need to make sure all your
layout‘schild blocksdefinitions are properly copied and that you got everything.At some pt its really much easier to just hack magento and make sure you record the hacks down somewhere for future upgrades.
Hey thats wat a test server is for isnt it? =)
July 30th, 2009 at 12:29 pm
This is exactly why I dont’ like updates.. I am not even sure I’ll regularly update my Magento store (when it’s done)… You make something work, and then some minor (or not so minor) change breaks it completely.
And also I agree with everyone here, this tut is one of the best out there regarding Magento!!! (I just finished all 9 steps). THANKS!!
One thing I still need to figure it out is when to write models with Capitals and when not to.
Perhaps just a question, I am thinking of adding some attributes to Customer admin and I don’t want to change core files. Is there a way to create a block in admin that I could use to extend/append to existing Customer Module without harming existing one? Any thoughts on this?
Cheers
July 23rd, 2009 at 12:20 pm
Nice article – thank you! I wish there was more on this from the Magento team.
As I look into existing Magento extensions most make use of the adminhtml namespace instead of creating the AdminController as listed in this article. Anyone know of another article explaining the adminhtml extension mechanics?
July 6th, 2009 at 11:18 am
what does your url look like?
it should be /twits/admin right?
July 6th, 2009 at 7:57 am
Please ignore my previous message, I had a language installed so it’s quite obvious why it wasn’t showing in the menu, silly me! However, when I click the link (I see a key is made so thats not the problem), I get redirected to the dashboard url.
July 6th, 2009 at 5:45 am
I just don’t know whats wrong with the following code, using magento 1.3.1, but the Adverts menu just won’t display in the adminpanel
1.0.0
ISAAC_Adverts_Block
ISAAC_Adverts_Helper
Adverts
100
adverts/admin
standard
ISAAC_Adverts
adverts
June 25th, 2009 at 1:52 pm
Since Magento 1.3, they’ve added a security feature that doesnt allow backend controller urls to be access directly. The key has to be provided. As such, even if you do the rewrite, you’ll still need the menu to generate the key for you, then click on the menu to access it. This feature is to prevent the CSRF attacks. Try it with the key!
June 25th, 2009 at 8:23 am
Great stuff guys! However, when I try to access my admin page (index.php/default/twit/admin & index.php/default/twit/admin/index) I just see the dashboard page. I just don’t know what I’m doing wrong, any ideas? I’ve added the rewite etc… I’m running on 1.3.1
June 18th, 2009 at 12:15 pm
Brilliant!
I couldn’t get the admin controller to respond on twit/admin for version 1.3.2, but this amend works perfectly – thanks Gary.
June 18th, 2009 at 11:03 am
Hi guys,
i’ve crafted the tutorial to make use of twit/admin instead of the other way around. Cause its simpler. /admin is bound to the adminhtml module.
However if your really find a need to do so:
add the following to your
etc/config.xml:In addition, change your menu to use
adminhtml/twitaction instead. That should work. Code as follows:Note that you can only use the admin menu to access your page. Because the
admincontrollercode is secured against CSRF. =D. Hope it works for you, i’ve tested it and it does for me.June 18th, 2009 at 7:44 am
Apologies – full repost without angle brackets…
Hi Claudia, I’ve experienced the same problem as you. Like you I disabled rewrites as I’m using Magento 1.3.2. Your xml snippet didn’t work for me as is and I though perhaps the [twit] tag should have been [SavantDegrees_Twit] (in keeping with the [frontend] xml structure). This didn’t work either, so I tried replacing your [admin] tag with [adminhtml], since [adminhtml] is the only [menu] surrounding tag that works for me to render the menu. No luck. So I tried changing your [use] tag contents from “admin” to “adminhtml”. No luck, so I tried all permutations thereof, all of which produce a 404 error:
No rewite, [admin], [routers], [SavantDegrees_Twit], [use] admin
No rewite, [admin], [routers], [SavantDegrees_Twit], [use] adminhtml
No rewite, [admin], [routers], [twit], [use] admin
No rewite, [admin], [routers], [twit], [use] adminhtml
No rewite, [adminhtml], [routers], [SavantDegrees_Twit], [use] admin
No rewite, [adminhtml], [routers], [SavantDegrees_Twit], [use] adminhtml
No rewite, [adminhtml], [routers], [twit], [use] admin
No rewite, [adminhtml], [routers], [twit], [use] adminhtml
The menu url generated in the admin screens (against which I tested) is http://magento.localhost.com/index.php/admin/twit/index/key/5f6a15dbc0a58347cade8abe91243946/
Do you have any suggestions, or could you post your full config.xml? Thanks Jon.
June 18th, 2009 at 7:40 am
Arghh – permutations were:
No rewite, admin, routers, SavantDegrees_Twit, use admin
No rewite, admin, routers, SavantDegrees_Twit, use adminhtml
No rewite, admin, routers, twit, use admin
No rewite, admin, routers, twit, use adminhtml
No rewite, adminhtml, routers, SavantDegrees_Twit, use admin
No rewite, adminhtml, routers, SavantDegrees_Twit, use adminhtml
No rewite, adminhtml, routers, twit, use admin
No rewite, adminhtml, routers, twit, use adminhtml
June 18th, 2009 at 7:37 am
Hi Claudia, I’ve experienced the same problem as you. Like you I disabled rewrites as I’m using Magento 1.3.2. Your xml snippet didn’t work for me as is and I though perhaps the tag should have been (in keeping with the xml structure). This didn’t work either, so I tried replacing your tag with , since is the only surrounding tag that works for me to render the menu. No luck. So I tried changing your tag contents from “admin” to “adminhtml”. No luck, so I tried all permutations thereof, all of which produce a 404 error:
No rewite, admin
No rewite, adminhtml
No rewite, admin
No rewite, adminhtml
No rewite, admin
No rewite, adminhtml
No rewite, admin
No rewite, adminhtml
The menu url generated in the admin screens (against which I tested) is http://magento.localhost.com/index.php/admin/twit/index/key/5f6a15dbc0a58347cade8abe91243946/
Do you have any suggestions, or could you post your full config.xml? Thanks Jon.
June 17th, 2009 at 8:47 am
Arghh
Here hopefully the full xml tags..
June 17th, 2009 at 8:44 am
On Magento 1.3.2.1 I had to add an admin router section as well, otherwise I got a 404 in the admin part. Might be because I played around a lot with my system, but I thought I’d rather mention it here.
I added this section (same as the front end router, only with ‘admin’ in the section)
admin
SavantDegrees_Twit
twit
June 1st, 2009 at 8:19 pm
Hello,
this didn’t work for me either.
I copy/pasted your code into my config.xml exactly (obviously, replacing the SavantDegrees and Twit) and what it does is take me to a blank page.
Have you encountered this issue?
Thanks!
May 28th, 2009 at 6:02 pm
In 1.2, we got the “admin/twit/” module link working by using
adminhtml/twit/
Can’t say it enough but thank you so much for this tutorial. After way too many wasted hours in Magento docs, books, and forums trying to learn how to work within Magento, your tutorial has helped us regain our sanity. Thanks a million!!!
May 21st, 2009 at 2:20 pm
Hey, thanks for these great tutorials.
I ran into the same problem as Jim with 1.3.1.1. The solution seems to be to change admin/twit to adminhtml/twit.
April 12th, 2009 at 8:11 am
Good point, i believe in 1.2 you’d use
instead.
I’m not convinced that you can bind it to admin/twit because it tries to look for the specific controller instead of trying to load a URL.
I’ll update the site if i can find reference to code that launches a url instead of a controller.
April 12th, 2009 at 7:58 am
Hi Gawee,
Im using 1.2 and the above code gives me something strange.. on the admin menu the module link is created for twit however the actual link is index.php//twit … whereas it should be index.php/admin/twit .. as you mention above, in 1.2 I can just manually type admin/twit and it works fine but I’d be interested in how to get the menu to work properly in 1.2 …
any thoughts ?
cheers
April 2nd, 2009 at 4:29 am
Hi i’ve update the portion for magento 1.3 and explained the problem. =D Will update the tutorial code shortly. Thanks for pointing this out!
April 1st, 2009 at 6:32 am
Hmm. Unfortunately this part does not work for me. If I go to the url test.lan/index.php/admin/twit, I am always redirected to
test.lan/index.php/twit/index/index/key/4bbdfae4c07fe2679371b232e3fc78b4/.
For testing I removed the rewriting part in the config.xml, but the same still happens when trying /index.php/twit/admin.
To be on the safe side I used the zip file that you provide but no luck either. By the way: I think that the file SavantDegrees_All.xml is in the wrong folder – in you zip file it is in /app/etc whereas according to your tutorial it should be in /app/etc/modules.
The front end part is working fine. I assume that this might have something to do with the new security feature of magento 1.3, but I don’t know that much about the inner workings of magento yet.
Any ideas or tips where I could look to solve the problem?
Thank you
Claudia