Recent Posts


Magento Front Controller


Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

This entry is part 1 of 43 in the series Miscellaneous Magento Articles. Later posts include Reinstalling Magento Modules, Clearing the Magento Cache, Magento's Class Instantiation Abstraction and Autoload, Magento Development Environment, Logging Magento's Controller Dispatch, Magento Configuration Lint, Slides from Magento Developer's Paradise, Generated Magento Model Code, Magento Knowledge Base, Magento Connect Role Directories, Magento Base Directories, PHP Error Handling and Magento Developer Mode, Magento Compiler Mode, Magento: Standard OOP Still Applies, Magento: Debugging with Varien Object, Generating Google Sitemaps in Magento, IE9 fix for Magento, Magento's Many 404 Pages, Magento Quickies, Commerce Bug in Magento CE 1.6, Welcome to Magento: Pre-Innovate, Magento's Global Variable Design Patterns, Magento 2: Factory Pattern and Class Rewrites, Magento Block Lifecycle Methods, Goodnight and Goodluck, Magento Attribute Migration Generator, Fixing Magento Flat Collections with Chaos, Pulse Storm Launcher in Magento Connect, StackExchange and the Year of the Site Builder, Scaling Magento at Copious, Incremental Migration Scripts in Magento, A Better Magento 404 Page, Anatomy of the Magento PHP 5.4 Patch, Validating a Magento Connect Extension, Magento Cross Area Sessions, Review of Grokking Magento, Imagine 2014: Magento 1.9 Infinite Theme Fallback, Magento Ultimate Module Creator Review, Magento Imagine 2014: Parent/Child Themes, Early Magento Session Instantiation is Harmful, Using Squid for Local Hostnames on iPads, and Magento, Varnish, and Turpentine.

Cocoa class has been put on hold for a while as I sling the code to pay the bills. I’ve been working a lot with the Magento commerce system lately, and thought I’d share. The following is from an answer I gave on Stack Overflow to the question.

How does Magento Code work?

First and foremost, I highly recommend you buy the PDF/E-Book from PHP Architect. It’s $20, but is the only straight forward “Here’s how Magento works” resource I’ve been able to find.

Second, if you have a choice, and aren’t an experienced programmer/don’t have access to an experienced programmer (ideally in PHP AND Java), pick another cart. Magento is well engineered, but it was engineered to be a shopping cart solution that other programmers can build modules on top of. It was not engineered to be easily understood by people who are smart, but aren’t programmers.

Third, Magento MVC is very different from the Rails/Django/Code Igniter/Cake/etc MVC model that’s popular with PHP developers these days. I think it’s based on the Zend model, and the whole thing is very Java OOP-like. There’s two controllers you need to be concerned about. The module/frontName controller, and then the MVC controller.

Fourth, the Magento application itself is built using the same module system you’ll be using, so poking around the core code is a useful learning tactic. Also, a lot of what you’ll be doing with Magento is overriding existing classes. What I’m covering here is creating new functionality, not overriding. Keep this in mind when you’re looking at the code samples out there.

I’m going to start with your first question, showing you how to setup a controller/router to respond to a specific URL. This will be a small novel. I might have time later for the model/template related topics, but for now, I don’t. I will, however, briefly speak to your SQL question.

Magento uses a EAV database architecture. Whenever possible, try to use the model objects the system provides to get the information you need. I know it’s all there in the SQL tables, but it’s best not to think of grabbing data using raw SQL queries, or you’ll go mad.

Final disclaimer. I’ve been using Magento for about two or three weeks, so caveat emptor. This is an exercise to get this straight in my head as much as it is to help StackOverflow.

Create a Module

All additions and customizations to Magento are done through modules. So, the first thing you’ll need to do is create a new module. Create an XML file in app/modules named as follows

cd /path/to/store/app
touch etc/modules/MyCompanyName_HelloWorld.xml    

<?xml version="1.0"?>    <config>

MyCompanyName is a unique namespace for your modifications, it doesn’t have to be your company’s name, but that the recommended convention my magento. HelloWorld is the name of your module.

Clear the Application Cache

Now that the module file is in place, we’ll need to let magento know about it (and check our work). In the admin application

  1. Go to System->Cache Management
  2. Select Refresh from the All Cache menu
  3. Click Save Cache settings

Now, we make sure that magento knows about the module

  1. Go to System->Configuration
  2. Click Advanced
  3. In the “Disable modules output” setting box, look for your new module named “MyCompanyName_HelloWorld”

If you can live with the performance slow down, you might want to turn off the application cache while developing/learning. Nothing is more frustrating then forgetting the clear out the cache and wondering why your changes aren’t showing up.

Setup the Directory Structure

Next, we’ll need to setup a directory structure for the module. You won’t need all these directories, but there’s no harm in setting them all up now.

mkdir -p app/code/local/MyCompanyName/HelloWorld
mkdir -p app/code/local/MyCompanyName/HelloWorld/Block
mkdir -p app/code/local/MyCompanyName/HelloWorld/controllers
mkdir -p app/code/local/MyCompanyName/HelloWorld/Model
mkdir -p app/code/local/MyCompanyName/HelloWorld/Helper
mkdir -p app/code/local/MyCompanyName/HelloWorld/etc
mkdir -p app/code/local/MyCompanyName/HelloWorld/sql

And add a config file

touch app/code/local/MyCompanyName/HelloWorld/etc/config.xml

and inside the config file, add the following, which is essentially a “blank” config.

<?xml version="1.0"?>    <config>    

Oversimplifying things, this config file will let you tell Magento what code you want to run.

Setting up the Router

Next, we need to setup the module’s routers. This will let the system know that we’re handling any urls in the form of

So, in your config file, add the following section.

<!-- ... -->
            <!-- the <helloworld> tagname appears to be arbitrary, but by 
            convention is should match the frontName tag below-->
<!-- ... -->        

What you’re saying here is “any url with the frontName of helloworld …

should use the frontName controller MyCompanyName_HelloWorld”.

So, with the above config in place, when you load the helloworld page above, you’ll get a 404 page. That’s because we haven’t created a file for our controller. Let’s do that now.

touch app/code/local/MyCompanyName/HelloWorld/controllers/IndexController.php

Now try loading the page. Progress! Instead of a 404, you’ll get a PHP/Magento Exception

Controller file was loaded but class does not exist

So, open the file we just created, and paste in the following code. The name of the class needs to be based on the name you provided in your router.

class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo "We're echoing just to show that this is what's called, normally you'd have some kind of redirect going on here";

What we’ve just setup is the module/frontName controller. This is NOT the MVC controller. Try the following URL, and you’ll get a 404, even if you had a fooAction method in MyCompanyNameHelloWorldIndexController

So, to setup the MVC controller, create a new file, and enter the following code (Yes, this also extends the MageCoreControllerFrontAction controller, even though it’s not a Front Action. Yes, this is confusing, but I think it’s how things are done)

touch app/code/local/MyCompanyName/HelloWorld/controllers/FooController.php
class MyCompanyName_HelloWorld_FooController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo 'Foo Index Action';

    public function addAction(){
        echo 'Foo add Action';

    public function deleteAction(){
        echo 'Foo delete Action';

Yo should now be able to hit the following URLs and see the results of your echo statments

So, that should give you a basic idea on how Magento dispatches to a controller. From here I’d recommended poking at the existing Magento controller classes to see how models and the template/layout system should be used.

Like this article? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Read more about Magento
Originally published March 30, 2009
Series NavigationReinstalling Magento Modules >>

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 30th March 2009

email hidden; JavaScript is required