Magento Developper’s Guide (Lesson 2) – Create your own controller

5 octobre 2011

This tutorial is the 2nd of many tutorials , 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: http://yoursite.com/plugin/method/

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

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

<Pfay_Test>
   <active> true </active>
   <codePool> local </codePool>
</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:

class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action
{
   public function indexAction ()
   {
     echo 'test index';
   }
   public function mamethodeAction ()
   {
     echo 'test mymethod';
    }
}

Now if you go to http://yoursite.com/test/index 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:

<?xml version="1.0"?>
  <config>
     <modules>
        <Pfay_Test>
          <version>1.0.0</version>
        </Pfay_Test>
     </modules>
     <frontend>
       <routers>
          <routeurfrontend>
              <use>standard</use>
              <args>
                 <module>Pfay_Test</module>
                 <frontName>test</frontName>
              </args>
           </routeurfrontend>
       </routers>
    </frontend>
</config>

Explanation:

<modules>
  <Pfay_Test>
    <version>1.0.0</version>
   </Pfay_Test>
</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.

<routers>
     <routeurfrontend>
        <use>standard</use>
          <args>
             <module>Pfay_Test</module>
             <frontName>test</frontName>
          </args>
     </routeurfrontend>
</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

http://yoursite.com/test/index, 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.

http://yoursite.com/routeurfrontend/index/

Now test your module:

http://yoursite.com/test/index 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… ;)

find the summary of this tutorial

  • No Related Post
ashwini # 26 octobre, 2011

could not see any output

# 31 octobre, 2011 zaira

Hi, Pierre Fay. I do found your tutorial very very good. Good for me who just started magento. No, i have some questions:

In /app/etc/, i saw a folder name modules. Now you stated here  » 2 – Enable the module in Magento:

Add a file Pfay_all.xml /app/etc/module/ « . Is it module folder or modules? I’m quiet confuse on this.

Now, when i finished all the steps that you stated in your tutorial. I got error 404 (Page not found). What could be the reason. I was working with in my local so i opened the page with this url: http://localhost/magento/test/index/

Any reason, why i cant view the page?

John # 31 octobre, 2011

The spaces in this made my module 404 error.

true
local

# 2 novembre, 2011 admin

@zaira: « module » is the french word for « plugin » i just forgot to translate the Title. but you enable the plugin by adding this xml file to app/etc/modules/

If you can’t see the change, first of all flush your cache. and if it doesn’t work check the syntax of the function’s name and files. The tutorial work so it must work for you too ;)

jonathan # 16 novembre, 2011

hi,
i’m a complete beginner to magento, and a little rusty with php.
i’m not getting past the 404 stage, though initially following the M4U blogger’s helloworld steps i did get a template page but without anything in the main body of it.
can you suggest whether the fact that i built the magento folder (using the downloader wizard) directly into the /var/www root, so when M4U says to navigate to http://servername/projectname/helloworld i don’t have a projectname component to my path (i.e. i browse to http://servername/index.php to start my magento).
thanks if you can understand this and advise if it is a problem.
jonathan

# 25 novembre, 2011 Pierre FAY

Hi Jonathan, if you have a blank page without 404 error i think it’s just normal. Depends what you’re doing in your template. to debug it :

make a die(‘test’); in your controller function and see if it’s displayed.
if not : have you declared your plugin in app/etc/module ?
if it’s ok, you must have a problem with your layout.

I hope it will help you.

Look at this tutorials, if you strictly follow it, it will work. ;)

Rohit # 29 novembre, 2011

Hi,
The tutorial is very good.It almost worked for me except for the part  » You can also access it using your router in place of frontName.

http://yoursite.com/routeurfrontend/index/  »

Can you explain why this part is not working.

Thank you

# 29 novembre, 2011 Pierre FAY

Hi Rohit ! Thanks :) hum..i don’t khnow why it’s not working for you, try to experiment to change the config.xml by yourself to ensure that you understand all the structure and the naming of the elements. Good Luck :)

Jorja # 5 décembre, 2011

I feel siatisfed after reading that one.

# 6 décembre, 2011 Paolo

Hi.
First of all , thank you for this simple and clear tutorial.
Great work !
I just started with magento and this is just what I needed to speed up my learning curve.

There is a small mistake in your controller :

Pfay_Test_IndexController class extends Mage_Core_Controller_Front_Action

Should be :

class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action

Cheers !

Paolo # 6 décembre, 2011

Just one other note ;
I figured it out , but it could be even more clearer if you put at the end of the article also the link and idea of :
MAGENTOSHOP/frontName/actionControllerName/actionMethodName/

So
MAGENTOSHOP/test/index/mamethode
to call other actions inside your index controller.

# 16 décembre, 2011 Spawn

This tutorial worked for me. Thanks

BTW, I’ve change the IndexController.php to work on magento 1.6

here it is,

PS.
Be careful about copy/paste and spaces

Pierre FAY # 16 décembre, 2011

Thanks Spawn, what did you do for magento 1.6 ? I’m currently using this and it work fine for me.

# 19 décembre, 2011 Gershon

Hey first nice job!
Just the basic syntax here is wrong i am starting off:

Pfay_Test_IndexController class extends Mage_Core_Controller_Front_Action

should be
class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action

Pierre FAY # 19 décembre, 2011

i’ve fixed the problem, thanks for reporting this Gershon ;)

# 9 janvier, 2012 pardeep kumar

Hi,

There is some extra spaces in Pfay_all.xml so remove it and have to use tag in contoller class in order to work this code. Now i believe it’ll solve your problem.

Roy Vincent # 6 février, 2012

OMG this tutorial is totally AWESOME!

# 17 février, 2012 vivek

hey all,

there are extra spaces in Pfay_all.xml file around « true » and « local ».

Thats why you are getting 404 page not found error, once you correct changes then it’ll work properly.

Thanx to pardeep kumar.

Thomas # 21 février, 2012

I had some errors too. I fixed them and all is working on magento 1.6.2.

The problems i fixed by :

- I added php tags (  » to IndexController.php
- I placed Pfay_all.xml in the modules (extra s) folder , (/app/etc/modules/)
- removed the spaces in Pfay_all.xml (before and after ‘true’ and ‘local’)
-flushed my magento cache.

and it works now. except the http://yoursite.com/routeurfrontend/index/ is still not working.

# 25 février, 2012 Wun Wei

Hi,

I previously developed my magento on a platform called, ‘WampServer’ (windows). It works fine. I am able to create webpages, upload products, and all other magento features.

However, i can’t seem to get through lesson 2 in your tutorial. I followed exactly everything from Lesson 1 until 2. I even created folders and files which previously don’t exist. e.g.

Computer>OS(C:)>wamp>www>magento>app>code>local>Pfay>Test>controllers>IndexController.php

..>Pfay>Test>etc>config.xml>

..>app>etc>config.xml

..>app>etc>modules>Pfay_all.xml

*Above are files paths that consist files with codes. Paths with empty folders are not listed.*

I kept getting 404 not found error, or the ‘request cannot be requested’. My magento shop is running fine when i got this error, so i’m sure everything else are online and operating well.
I faced similar problems from the guys who commented above and tried the suggested solutions but still doesn’t solve it.

I don’t know if i’m providing you enough details to understand my current situation, but if you wish, i can personally email you to explain further.

Hope to hear from you soon. Thanks!

Col SC Sood (Retd), Professor # 12 mars, 2012

A very nice tutorial. I am a beginner for magento. English grammar needs to be improved. Corrections for « module » to « modules » , tags to IndexController.php, must be updated.
I could not run http://yoursite.com/routeurfrontend/index/ . Regards and wish you all the best.

# 21 mars, 2012 Jafer Balti

Hi Pierre FAY Thanks a lot for such a nice tutorial.
the link which worked for me is

http://localhost/magento/index.php/test/index/
instead of
http://localhost/magento/test/index/

rohit goel # 9 avril, 2012

hi ,thius is realy a very good and easy to understand tutorial, i have triend so many sites for this but nothing was satisfactory ,but here i got realy very good tutorial
thanks a l;ot and keeo this great working going on

# 4 mai, 2012 Cindy

Hi Pierre,
What version for using this tutorial works ?

I’m using magento 1.7 and always got error 404 (Page not found)

Pierre FAY # 10 mai, 2012

@Cindy it must worked for Magento 1.7 ;)

# 19 mai, 2012 Daniel R

need to add <?php to the php file

Swapdude # 22 mai, 2012

If code is correct in all files and still you get 404 error page try to clear magento cache (by deleting everything inside /var/cache/ directory in main directory).

Cheers :)

# 4 juin, 2012 Herb

Hi, great tutorial.

I’m using 1.7, and I also get 404 error.

However, like Jafer Balti,

the link which worked for me is

http://localhost/magento/index.php/test/index/

instead of

http://localhost/magento/test/index/

desbest # 18 juin, 2012

I’ve completed the whole tutorial with Magento 1.7 with the cache off.

And still when I load up magento/test/index in my browser, I get a 404 not found. Please help as it’s not working.

# 18 juin, 2012 desbest

Correction:
The website address should not contain « Test », but instead should be « test ».

There should be no uppercases for the modules.

Please put that in the tutorial so I won’t make mistakes on it.

anonymous # 18 juin, 2012

Hi,

Did the steps and I end up with a 404 error.

I am using Linux and Magento 1.7 with Apache )Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze13 with Suhosin-Patch configured).

solved: third comment mentions « true local »

true
local

has to be

true
local

Regards

# 19 juin, 2012 anonymous

The code of the controller lacks

zuber # 20 juin, 2012

Good job

# 27 juin, 2012 zippp

Tutorial is very good, but could you please remove the spaces around ‘true’ and ‘local’ in the Pfay_all.xml, it causes the 404 error.
Thanks a lot for a great material to study :)

Me # 28 juin, 2012

Jafer Balti THANK YOU SO MUCH!
I had been looking for an answer at least 40 minutes…
Triple checked eeverything and finally, a simple solution. Thanks again.

# 13 juillet, 2012 Tobi

Finally a really good tutorial!

Marcin # 18 juillet, 2012

That’s a really great tutorial. You’ll save your readers some time if you will:
- add ‘<?php' to the IndexController.php
- remove the spaces in Pfay_all.xml – it causes the 404 page

Accessing the page at routeurfrontend/index/ didn't work for me.

Thanks again!

# 10 août, 2012 VB

Hi there,
please note that the tutorial says to write « Pfay_all.xml »
After that, it says that the file should be named « Pfay_All.xml ».
The right version is the second one, with capital letters.
Thanks for the tutorial!!

nubeiro # 4 septembre, 2012

Thanks a lot … it wasn’t working for me either, until I realized that I had a wrong controller file: « indexController.php » instead of « IndexController.php »

# 3 octobre, 2012 Tahmina Khatoon

wow! Excellent. Working nice.

Netesh # 23 octobre, 2012

Nice tutorial. its working for me

# 26 novembre, 2012 Jamal Ashraf

Hi, I have no words in my dictionary which will fit to praise you. You are just awesome. Writing such a nice tutorial with so much effort is priceless. I am a php developer and was just stunned looking at the efforts you put in writing this tutorials.

Vithal Bariya # 12 décembre, 2012

hi guys

there is problem in Pfay_all.xml so remove space in
true or local another copy and past this line

true
local

:)

# 16 décembre, 2012 Vishal Chanana

Thanks , Pierre FAY

I create my first module in magento.

Your tutorial really very easy to understand.

Vithal Bariya # 20 décembre, 2012

check your browser link match this path

http://yoursite.com/test/index

so tray this link

http://yoursite.com/index.php/test/index

# 22 décembre, 2012 magentoNewb

when i type this
http://192.168.0.10/~abc/magento/test/index
i am getting the error

404 not found and its saying

The requested URL /home/abc/www/magento/index.php was not found on this server.

whereas when i type
http://192.168.0.10/~abc/magento/index.php/test/index
it is giving page not found
but i guess it is of no use as it is calling index.php and anything typed after it if doen’t exist and it will give the same error.

Please guide me through this

Pierre FAY # 24 décembre, 2012

hi magentoNewb, you must activate url rewriting on your website :) i’m not realy understand your problem but i can seen you can’t use magento because of installation. look at your .htaccess..maybe it’s around this or apache vhosts rules :) good luck :)

# 24 décembre, 2012 Pierre FAY

Thanks Vishal :) if you want to do all this very quickly and lot more (2days of development in 5minutes :) ) go on generateplugin.com. it’s a tool i’ve developed. you can use the discount « PIERREFAYEN » at checkout to get -40% discount. Have a nice day and thanks for the comment :)

Vous aussi donnez votre avis
Votre nom : (requis)
Votre email :
Votre message :