This tutorial is the 2nd in a series, you will learn how to create your controller Magento. If you have not read the first articles, I strongly advise you to do so.
We will now create a controller. A controller is a PHP class that contains functions accessible from a URL. When you type the URL into your browser, you actually go into the function of a controller for this url.
This function's instructions will be executed and magento will return the result to the user browser. The URL is structured as follows: http://yourwebsite.com/module/methode/
So You begin by creating your module:
1- Create the folder/app/code/local/Pfay/Films/
This will create your namespace Pfay
and your module Films
Then you have to tell the Films magento module contained in your namespace Pfay exists and must be loaded. That is why we add a file called Pfay_Films.xml in the/app/etc/modules/.
This folder contains all statements magento modules, some files such as Mage_All.xml used to group several statements modules. Here we declare several modules namespace "Mage". It could very well use Pfay_All.xml instead of Pfay_Films.xml to declare our module but as we create a module in this tutorial, this is not really useful to do so.
2- Activate the module in Magento:
Add a Pfay_Films.xml
And insert the following code in it
This file makes it possible to declare a magento module that is present in the namespace "Pfay" and it's called himself "Films". You put it between the tags "modules" :
allows you to declare the Films module and your Pfay your namespace.
true = the module is active, false = module is not enabled
The module will be in the Local
codePool (the folder in app/code/ is named "the codePool")
Magento thus find the files of this module in the /app/code/local/Pfay/Films/
. If the local file does not exist, create it.
Now create the controller:
The controllers will always be in the "controllers" folder of our module and will have a name that finish with "Controller". Example: IndexController.php, TotoController.php, notice the capital letters
Our controller is a class that extends Mage_Core_Controller_Front_Action
Each method will end with the keyword "Action" to access it from a url. notice the capital letters
We will call these methods "actions".
1- Create the folder/app/code/local/Pfay/Films/controllers/
2- Inside create a file IndexController.php
class Pfay_Films_IndexController extends Mage_Core_Controller_Front_Action
public function indexAction()
echo 'test index' ;
public function mamethodeAction()
echo 'test mamethode';
Now if you go to http://yourwebsite.com/films/index this happening?
Sure ... you can not find the page, there is an error because you have not said magento where it can find your controller and with which url you can access it.
Config.xml file to configure your module:
In the same way you declared your module for Magento to takes into account the Pfay_Films.xml file, create a file etc/config.xml to declare everything that is in our module.
In the etc/config.xml of your module, we will declare our controller.
Magento will find your Pfay_Films.xml file, it will go in your/app/code/local/Pfay/Films/etc/and will read config.xml to know where to get your controller and how your module.
So create in your Films
, with in config.xml
In this part, the identity of the module is declared. Magento can verify that the module you said (in your Pfay_All.xml file) is the one there and the version number allows you the updates of your module in the future (we 'll see how to do it in a future lesson).
This code allows you to declare a router "routeurfrontend" which is actually the route used by Magento to access your controller.
In our example, the router is a "standard" one (that says it is a module that will be displayed on the frontend).
The module name is Pfay_Films and the shortcut to access it via the URL is movies and when you will type: http://yourwebsite.com/films/index we arrive on the index method of your controllerIndexController.php (of your controllers folder).
Now test your module:
/index returns you "test index" (you can add "/index" at the end if you want to call the index method, but by default, if nothing is set as action parameter in the url, it calls the method that will be called "indexAction" in your controller)
/index/mamethode refer you "test my method"
Congratulation ! You now know how to create a controller in magento
. Now try to understand this by repeating it at home and you should be fine to move on the next lesson.