3
votes

I'm working on a module for the admin area of Magento. I'm trying to follow Alan Storm's tutorial on Magento admin controllers but can't seem to get my controller to do anything. I think it may have something to do with routing, but I'm not sure. It shows me the frontend template with a 404 error.

(Note: I have included all relevant code here. The actual question is at the very bottom.)

The module is called Mynamespace_Donor and lives in app/code/local/Mynamespace/Donor/.

My etc/config.xml looks like this:

<?xml version="1.0"?>
<config>
    <modules>
        <Mynamespace_Donor>
            <version>0.1.0</version>
        </Mynamespace_Donor>
    </modules>
    <global>
        <helpers>
            <donor>
                <class>Mynamespace_Donor_Helper</class>
            </donor>
        </helpers>
        <resources>
            <donor_setup>
                <setup>
                    <module>Mynamespace_Donor</module>
                </setup>
            </donor_setup>
        </resources>
    </global>

    <admin>
        <routers>
            <donor>
                <use>admin</use>
                <args>
                    <module>Mynamespace_Donor</module>
                    <frontname>donor</frontname>
                </args>
            </donor>
        </routers>
    </admin>

    <adminhtml>
        <menu>
            <donor translate="title" module="donor">
                <title>Donor</title>
                <sort_order>42</sort_order>
                <children>
                    <manage_donors module="donor">
                        <title>Manage Donors</title>
                        <action>donor/index/index</action>
                    </manage_donors>
                </children>
            </donor>
        </menu>
    </adminhtml>
</config>

And my controllers/IndexController.php looks like this:

<?php
class Mynamespace_Donor_IndexController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout();

        //create a text block with the name of "example-block"
        $block = $this->getLayout()
        ->createBlock('core/text', 'example-block')
        ->setText('<h1>This is a text block</h1>');

        $this->_addContent($block);

        $this->renderLayout();
    }
}

The menu item points me to /index.php/donor/index/index/key/e98a... which shows a 404 page. When I try to go directly to /donor, /index.php/donor, /index.php/donor/index, etc I still get 404 errors.

If I remove the <helpers> from the config, Magento complains that it can't find it. If I remove the <adminhtml> section, it stops complaining, even though I still have my <admin><routers> section in there (don't know if the routing stuff needs a helper or if this is even relevant).

I've also tried adding this block under <adminhtml>, but it when I try editing role permissions I get a white page with this error: Fatal error: Class 'Mage_Mynamespace_Donor_Helper_Data' not found in /home/mysite/public_html/magento_dev_1_10/app/Mage.php on line 520

<acl>
    <resources>
        <admin>
            <children>
                <donor translate="title" module="Mynamespace_Donor">
                    <title>Donors</title>
                    <sort_order>60</sort_order>
                    <children>
                        <manage_donors>
                            <title>Manage Donors</title>
                        </manage_donors>
                    </children>
                </donor>
            </children>
        </admin>
    </resources>
</acl>

Question: What am I doing wrong here? Why can I not access this controller?

And lastly, in Alan's sample code, the URL began with the module name, but I would like mine to start with /admin/donor instead of /donor. What changes do I need to make for this?


Edit 1 The ultimate goal is to have a new tab in the admin area for managing Donors and related data in the system. When you click on some menu item, I'd like to show a grid and have sub-tabs and stuff like that. I'm not looking to override the adminhtml controller - although I am extending it per Alan's suggestion:

The only difference from a standard controller here is that we’re extending Mage_Adminhtml_Controller_Action instead of Mage_Core_Controller_Varien_Action. Mage_Adminhtml_Controller_Action contains important code for validating the admin session, as well as several methods that are useful in an Admin Console context.

So in this picture, clicking that first sub-menu item should call the Index action of my controller and show a grid or something to manage the Donors.

The menu

3
Please consider using the module creator to build all this config stuff for you. It saves you from masses of boilerplater and (from memory) even has a template for an admin page with a grid. - clockworkgeek
Thanks for the tip, I'll certainly start with that next time :) Or maybe if I get stuck on something else.. - Colin O'Dell

3 Answers

7
votes

Try to replace :

<frontname>donor</frontname>

with :

<frontName>donor</frontName>

That will fix your 404 error.

5
votes
<config>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <mynamespace_donor before="Mage_Adminhtml">Mynamespace_Donor</mynamespace_donor>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Try the above in your config.xml

More details:

http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/

2
votes

Regarding the fatal error; You have module="Mynamespace_Donor" in the permissions. If you look at Alan's tutorial he has:

<tutorial_menu translate="title" module="adminhelloworld">

He uses the module alias instead of the module name - adminhelloworld instead of Alanstormdotcom_Adminhelloworld. Magento is trying to find the helper relevant for translating the title. In your case you need:

<donor translate="title" module="donor">

The donor comes from your own config file:

<helpers>
    <donor> <!-- <<< This is the alias "donor" -->
        <class>Mynamespace_Donor_Helper</class>
    </donor>
</helpers>