Create a magento controller

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/app/etc/Module/ And insert the following code in it

<?xml version="1.0"?>
   <config>
      <modules>
        <Pfay_Films>
            <active>true</active>
            <codePool>local</codePool>
         </Pfay_Films>
       </modules>
    </config>
    
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" :

<Pfay_Films>
   <active>true</active>
   <codePool>local</codePool>
</Pfay_Films>

Pfay_Films allows you to declare the Films module and your Pfay your namespace. Active: true = the module is active, false = module is not enabled codePool: 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".
Go to/app/code/local/Pfay/Films/:

1- Create the folder/app/code/local/Pfay/Films/controllers/
2- Inside create a file IndexController.php which contains:

<?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 folder etc/, with in config.xml. config.xml contain:

<?xml version="1.0"?>
  <config>
     <modules>
        <Pfay_Films>
          <version>0.1.0</version>
        </Pfay_Films>
     </modules>
     <frontend>
       <routers>
          <routeurfrontend>
              <use>standard</use>
              <args>
                 <module>Pfay_Films</module>
                 <frontName>films</frontName>
              </args>
           </routeurfrontend>
       </routers>
    </frontend>
</config>

Explanation:

<modules>
  <Pfay_Films>
    <version>0.1.0</version>
   </Pfay_Films>
</modules>

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).

<routers>
     <routeurfrontend>
        <use>standard</use>
          <args>
             <module>Pfay_Films</module>
             <frontName>films</frontName>
          </args>
     </routeurfrontend>
</routers>

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: http://yourwebsite.com/films/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)

http://yourwebsite.com/films/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.
Available documents for this article :
Books that can help you :
  • Livre Magento Developer's Guide by Alan Mc Gregor
  • Livre Magento Performance Optimization
  • Livre Grokking Magento Vinai
Questions about this lesson
No questions for this lesson. Be the first !

You must be logged in to ask for help on a lesson.