Magento uses the Front Controller pattern to implement workflows for it’s application. It has a single entry point (
index.php) for all of it’s requests. Let’s see how it all works.
Magento leverages the class Mage_Core_Controller_Front_Action . A module’s controllers usually extend this base class.
Magento handles requests through URLS in the browser. The front controller helps to process these requests.
A url is broken up into multiple sections. A basic url has the following elements:
- A router
- A controller
- An action
A router is defined in your Magento module configuration xml. This is an alias for your module. It is the first section after your base URL.
A controller is a class used to help process your request. It is the second section after your base URL.
An action is the actual method called within your defined class. It is the third section after your base URL.
Here is a basic example of Magento’s Front Controller process.
http://www.mymagentosite.com/ – Base URL
/customer/ – Router for the Customer module
/account/ – Account controller class. This is located in the controllers folder of the Customer module and is the file
AccountController.php and calls the class
/login/ – The actual method called within Mage_Customer_AccountController . Whenever a method is called, standard Magento practice is to append the word ‘Action’ at the end, to differentiate between methods that parse files. The method called here is loginAction() .
Anything after the standard router/controller/method call is parsed any additional parameters are parsed as key=>value pairs to be handled with the request. For example:
This request would load the Sales module through the router, load the Order Controller and call the method viewAction() . It then passes the key/value pair of order_id=>24. You can fetch this value from calling the following within the controller:
// $order_id = 24;
$order_id = $this->getRequest()->getParam('order_id');
But what happens when you only see a url like this?
The other parts are missing! Whenever there are no other parts of a URL request, they are automatically filled in with an index parameter. So for this example,
is the same as
Which will pull the customer router from the module, load
IndexController.php and call
References for Front Controller:
Wikipedia: Front Controller Pattern