Wednesday , December 19 2018
Home >> All tutorial >> Magento Developers’ Guides – Part 2: Create your own controller

Magento Developers’ Guides – Part 2: Create your own controller

This tutorial is the 2nd of series “Create Magento Plugin”, you will now learn how to create your own controller in Magento. If you have not read the first articles yet, I strongly advise you to do so. 
With Magento, you can access to your controller with the url:

Start by creating your module:

1 – Create the folder /app/code/local/Pfay/Test/

This create your namespace Pfay and your module Test .

2 – Enable the module in Magento:

Add a file Pfay_all.xml /app/etc/module/

And insert the following code in it

  1. <?xml version = “1.0”?>
  2.    <config>
  3.       <modules>
  4.         <Pfay_Test>
  5.             <active> true </active>
  6.             <codePool> local </codePool>
  7.          </Pfay_Test>
  8.        </modules>
  9.     </config>
This file is used to declare all your modules in the Pfay namespace, you have to put it between the tags « modules » declaration of your plugin one after the other.
  1. <Pfay_Test>
  2.    <active> true </active>
  3.    <codePool> local </codePool>
  4. </Pfay_Test>

Pfay_Test allows you to declare the namespace of your plugin Test Pfay.
active: true = the plugin is active
false = the plugin is not enabled

codePool: The module will be in the local file because it is a module that we create ourselves (remember the lesson 1).
Magento will therefore find the files in that module in the /app/code/ local/Pfay/Test/

ahead and create the controller:

go to /app/code/local/Pfay/Test/

1 – Create the file /app/code/local/Pfay/Test/controllers/

2 – Inside create a IndexController.php which will contain:

  1. class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action
  2. {
  3.    public function indexAction ()
  4.    {
  5.      echo ‘test index’;
  6.    }
  7.    public function mamethodeAction ()
  8.    {
  9.      echo ‘test mymethod’;
  10.     }
  11. }

Now if you go to What’s happening?

Actually … you can not find the page, there’s an error because you have not already declare your controller in the plugin.

Config.xml file to configure your plugin:

In the same way that you have declared your plugin for Magento to take into account Pfay_All.xml, the file config.xml of your plugin will allow you to you declare the controller of your plugin.

Mangento will check Pfay_All.xml and find your file, it will go in your /app/code/local/Pfay/Test/etc/ and reads the config.xml file to see or pick up your controller.

So create your file Test , a record etc/ with in config.xml .

config.xml contains:

  1. <?xml version=“1.0”?>
  2.   <config>
  3.      <modules>
  4.         <Pfay_Test>
  5.           <version>1.0.0</version>
  6.         </Pfay_Test>
  7.      </modules>
  8.      <frontend>
  9.        <routers>
  10.           <routeurfrontend>
  11.               <use>standard</use>
  12.               <args>
  13.                  <module>Pfay_Test</module>
  14.                  <frontName>test</frontName>
  15.               </args>
  16.            </routeurfrontend>
  17.        </routers>
  18.     </frontend>
  19. </config>
  1. <modules>
  2.   <Pfay_Test>
  3.     <version>1.0.0</version>
  4.    </Pfay_Test>
  5. </modules>
In this section, we declare the identity of the module for magento module to verify that what you declare in your Pfay_All.xml is the good plugin and if the version number allow you to eventually have an update for your plugin.
  1. <routers>
  2.      <routeurfrontend>
  3.         <use>standard</use>
  4.           <args>
  5.              <module>Pfay_Test</module>
  6.              <frontName>test</frontName>
  7.           </args>
  8.      </routeurfrontend>
  9. </routers>

Allows you to declare a router « routeurfrontend » which is actually the road used by magento to access your controller.
In our example, the router is « standard » (it means that it is a module that will be displayed on the frontend).

The module name is Pfay_Test and the shortcut to access it via the url is test and when you will type, we arrive on the index method of your controller IndexController.php (in your folder controllers )

You can also access it using your router in place of frontName.

Now test your module: you can read « test index » (you can add /index at the end of your url if you want to call the index method but by default if nothing is specified it calls the method « index »)

You know now create a controller. Now try to understand this by doing it again and again at home and you should be able to move on the next tutorial.

If you have any questions, feel free to leave a comment. If you want to help me, share a link to this website with your friends on facebook, twitter etc… ;). Read more about Front Controller here.


Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics