Magento CE 184.108.40.206
Ultimate fix, you say? Well, after trying every Google result I could find and gaining an education on Alan Storm’s awesome blog, this is what worked for me in the end.
I’m building a custom module (extension, if you prefer). I want to have a new admin menu item, pointing to my new admin page. My controller is NOT called IndexController.php. Simple, yes? I’ve tried every config.xml setup possible and I still get a ‘Whoops’ 404 page in Magento.
First, let me define my folder structure for clarity. Please note; in each example here, I’m only going to show you the relevant info so as not to confuse.
Now, our config.xml file. For the uninitiated, this is really where the meat and potatoes of Magento configuration goes down, hence the name.
I prefer to use the above method for the routers definition (instead of this). According to their documentation, it is the ‘Magento’ way and also because the alternative has a track record of causing problems.
So, we have defined a simple admin router pointing to our controller by using the name jason_extendo. The link to this controller lives in the adminhtml.xml file.
<jason_extendo translate="title" module="jason_extendo">
<title>My Awesome Sauce Extension</title>
This XML creates a sub-menu item in admin, under the ‘Promotions’ menu (notice the ‘promo‘ node). The sub-menu item points first to our module using the frontName we defined (jason_extendo), then our controller (adminhtml_extendo) and then the controller action we want (index).
The controller would normally live in the controllers folder, duh. However, I want to add more frontend and backend (admin) controllers later on, so mine lives in a sub-folder called Adminhtml.
Now, let’s take a look at this controller. I’ve stripped out everything except the basics of what we’re trying to achieve here – routing.
class Jason_Extendo_Adminhtml_ExtendoController extends Mage_Adminhtml_Controller_Action
public function indexAction()
echo 'You beauty, it works!';
The Controller part of the filename and class name are absolutely key here. This is where so many other potential solutions to this problem fall down. Without defining the Controller part, Magento will effectively ignore this file, resulting in your 404 error. Onward!
Well, that’s it, actually! When you put this all together, push the code to your Magento installation, clear cache, logout, login, sip your coffee, blink, pee and test.
Here are a few general pointers to get you on the right track.
- Case is very important in Magento. For example, frontname and frontName are NOT the same thing.
- You could try putting a default file called IndexController.php in the controllers folder to see if a basic call to your page works, then build out from there.
- Strip out any complicated logic to see if you get to your controller(s), then put it all back piece by piece.
- Don’t use a closing PHP tag. Magento is built on Zend Framework and one does not use a closing tag in Zend. You would fail the exam 😉
- If you have a clump of your hair in each hand, try using a module creator to do all this for you.
- Take a break. Seriously, it helps!
Well done for trying to develop a module, it’s very rewarding when it all works. Keep cracking and happy coding, programmer!
Please do share this article.