Module Configuration and URL Structure

Magento is a configuration based MVC. There is one other type of MVC which is know as convention based mvc.

Convention based MVC – In this type of system if you want to add let say a controller or a model you will create a file/class and the system will automatically pick it up.

Configuration based MVC – In this type of system in addition to adding the new file/class to the codebase, you often need to explicitly tell the system about the new class, or new group of classes.

As Magento is a configuration based MVC so each module has a config.xml. This file contains all the relevant information about the module. At run time all the configuration files are loaded in one configuration tree. For Example, if you want to use models in your module you have to add some code to the config.xml file. The code will be as :

Template of Config.xml

Template of Config.xml

The above code will tell the Magento that the module want to use the models. It also tells what is the base class name for all your Models. Similarly you need to make changes or add some code to the config.xml if you want to add Helpers, Blocks, Routes for your Controllers, Event Handlers, and more.

URL Structure

In every MVC based php application the main entry point of the application is index.php but we do not code in the index.php file.In an MVC system, index.php will contains code/calls to code that does the following:

  1. Examine URL
  2. Based on some rules the URL will get converted to the controller class and action method.
  3. Instantiate the controller class and call action the method.

Lets explain this by taking a example URL: http://www.abc.com/helloworld/messages/welcome/user/guest

The URL after the server address is parsed as follows:

  1. Front name – helloworld
  2. Controller Name – messages
  3. Action name – welcome
  4. Parameter/Value – user/guest

Lets explain the above terms one by one

  1. Front name – helloworld
    The first part of the URL after server name is called as front name. This tells Magento in which module it can find the controller. In our example the front name is product which is a module located at app/code/local/Demo (Demo is the packagename)

    Module Located at app/code/local/Demo

    Module Located at app/code/local/Demo

  2. Controller Name – messages
    the second part of the URL tells Magento which controller it should use. Each module has the folder controller which contains all the controllers of that module. in our case the Magento will look for app/code/local/controllers/MessagesController.php which contains code like

    class Demo_Helloworld_MessagesController extends Mage_Core_Controller_Front_Action {
    
    }
    
  3. Action name – welcome
    This is the action name in the controller. In our example this is “welcome”. in the controller the action method will be name as welcomeAction which look like

    class Demo_Helloworld_MessagesController extends Mage_Core_Controller_Front_Action {
    public function welcomeAction()         
    	{
    
    	}
    }
    
  4. Parameter/Value – user/guest
    Any path portions after the action name will be considered key/value GET request variables. So, in our example, the “user/guest” means there will get a GET variable named “user”, with a value of “guest”.

This post tells us about the Module Configuration and URL Structure of Magento. In the next post we will create the new module in Magento.

Rajat Jain
Rajat Jain

Latest posts by Rajat Jain (see all)

Leave a Reply