Create the administrative complement
Next we’ll create the administration controller and configuration to achieve the https://127.0.0.1/magento/index.php/twit/admin reservation. This took me a long time to figure out how to do nicely.

  1. 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)
            Twit/ (Or whatever your module name might be)
              Block/
                - Index.php
              controllers/
                - AdminController.php
                - IndexController.php
              etc/
                - config.xml
              Helper/
                - Data.php
  2. Then we obviously create the adminController.php
    We’ll make the admin page reuse the same Hello world block that we’ve created in Phase 1.

    <?php
    class SavantDegrees_Twit_AdminController extends Mage_Adminhtml_Controller_Action
    {
        public function indexAction()
        {
    		$this->loadLayout()
    			->_addContent($this->getLayout()->createBlock('twit/index'))
    			->renderLayout();
        }
    }

    Note that this controller extends Mage_Adminhtml_Controller_Action instead. 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/twit/admin to see the page in action. Again, this produces our ubiquitous Hello world. You’d probably figured out by now that /twit refers to our frontend controller reservation by our config.xml and then /admin refers to the adminController that we’ve just created. Now you can try https://127.0.0.1/magento/index.php/twit/admin/index to see the indexAction method being called. Try it with other action names!

  3. Next we’ll do the config.xml magic that allows us to use https://127.0.0.1/magento/index.php/admin/twit for the administration.
    <?xml version="1.0"?>
    <config>
        <modules>
            <SavantDegrees_Twit>
                <version>0.1.0</version>
            </SavantDegrees_Twit>
        </modules>
        <global>
             <helpers>
                <twit><class>SavantDegrees_Twit_Helper</class></twit>
            </helpers>
    	<blocks>
                <twit>
                    <class>SavantDegrees_Twit_Block</class>
                </twit>
            </blocks>
        </global>
        <adminhtml>
            <menu>
                <twit translate="title" module="twit">
                    <title>Twits</title>
                    <sort_order>100</sort_order>
                    <action>twit/admin</action>
                </twit>
            </menu>
        </adminhtml>
        <frontend>
            <routers>
                <SavantDegrees_Twit>
                    <use>standard</use>
                    <args>
                        <module>SavantDegrees_Twit</module>
                        <frontName>twit</frontName>
                    </args>
                </SavantDegrees_Twit>
            </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 twit/admin page. This is vital for version 1.3 onwards as the menu generates the necessary url key to access the page.
  4. And finally the helper Data.php file
    <?php
    class SavantDegrees_Twit_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_Twit_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.

Moving along … Part 3 – Database

24 Responses to “Howto: Repackageable custom extension development in Magento – Part 2 – Admin Controller”

  1. Aswin S Says:

    Wow great dude.. For the first time I’m looking at a Magento tutorial thats written for a beginner like me.. Thanks

  2. Kat Says:

    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!!!

  3. gaweee Says:

    Hey Kat
    From what i understand you could extend the Controller and the Model then edit the view.
    But thats reallyyyyyyyy troublesome. you need to make sure all your layout’s child blocks definitions 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? =)

  4. Kat Says:

    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

  5. Ron Peled Says:

    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?

  6. gaweee Says:

    what does your url look like?
    it should be /twits/admin right?

  7. Sam Granger Says:

    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. :(

  8. Sam Granger Says:

    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

  9. gaweee Says:

    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!

  10. Sam Granger Says:

    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

  11. JonUK Says:

    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.

  12. gaweee Says:

    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:

       	<rewrite>
                <SavantDegrees_Twit>
                    <from><![CDATA[#^/admin/twit#]]></from>
                    <to>/twit/admin</to>
                </SavantDegrees_Twit>
            </rewrite>

    In addition, change your menu to use adminhtml/twit action instead. That should work. Code as follows:

        <adminhtml>
            <menu>
                <twit translate="title" module="twit">
                    <title>Twits</title>
                    <sort_order>100</sort_order>
                    <action>adminhtml/twit</action>
                </twit>
            </menu>
        </adminhtml>

    Note that you can only use the admin menu to access your page. Because the admincontroller code is secured against CSRF. =D. Hope it works for you, i’ve tested it and it does for me.

  13. JonUK Says:

    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.

  14. JonUK Says:

    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

  15. JonUK Says:

    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.

  16. Claudia Says:

    Arghh

    Here hopefully the full xml tags..

    <admin>
        <routers>
            <twit>
                <use>admin</use>
                <args>
                    <module>SavantDegrees_Twit</module>
                    <frontName>twit</frontName>
                </args>
            </twit>
        </routers>
    </admin>
  17. Claudia Says:

    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

  18. Helen Says:

    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!

  19. kelly Says:

    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!!!

  20. Jacob Says:

    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.

  21. gaweee Says:

    Good point, i believe in 1.2 you’d use

    <action>twit/admin/</action>

    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.

  22. jim Says:

    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 :)

  23. gaweee Says:

    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!

  24. Claudia Says:

    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

Leave a Reply