Lesson 1: Create a Controller – Magento Extension Tutorial

Hi Guys, This is the first lesson in this series of Magento module tutorials. I hope you have read the introduction post, if not please read it before continuing. Disable your cache and Magento compiler while doing any development. In the introduction post we have created a module named “First” in the namespace “Hashid“. So now you have the following path:

app/code/local/Hashid/First

Register the module

We have to register our module now so that Magento will know where our module exists. Go ahead and create the following file. Filename can be anything. Usually Namespace_All is used, which means settings of all modules in a namespace are given in this single file.

app/etc/modules/Hashid_All.xml

Write the following code in the file:

The above code is self explanatory. But still I will tell you in short:

<active> tag says if module is active or not. Values allowed are true and false

<codePool> tag says in which code pool we have placed our module.

Declare Controller in config.xml

Since we are going to create a controller in this lesson, let us first declare it in our config.xml file here:

Hashid/First/etc/config.xml

This file will contain all our declarations and explains the actions of our module.

Put the following code in the config.xml file:

Explanation:

<modules> tag helps Magento identify our module and the version number.

We create a frontend router named myrouter, it will be used as a road to reach our controller.

<uses>standard</uses> means that we are going to use frontend controller

<frontName>helloworld</frontName> means that the end point of our controller is “helloworld“. It is a shortcut to our module. We can access our module using the URL http://yoursite.com/helloworld

For me it is http://localhost/tutorial/helloworld since I’m working on my local server. Now the link will show you 404 error because we have not written the controller yet. Let’s write it!

Create Controller

Now lets go to the code section of our module and create our controller like this:

app/code/local/Hashid/First/controllers/IndexController.php

Insert the following code into it:

You can see the class name as Hashid_First_IndexController. In Magento the directory path is used to write class names. For example, if we have a folder Hashid/First/controllers/Adminhtml/IndexController.php, the class name would be Hashid_First_Adminhtml_IndexController.php. Don’t think too much now, you will understand slowly.

You can see the public function indexAction() in the class. In a controller the indexAction() is the default function which gets executed when controller is called. We can have other functions also, say hashidAction(), coolAction() etc.

Now open your browser and point to yoursite.com/helloworld , you can see our text getting printed like below. This is because the indexAction() is being called.

hello-world-controller-magento

Note that yoursite.com/helloworld is same as yoursite.com/helloworld/index/index

The first index means IndexController, and the second index means indexAction() .

So naturally, if you had a controller named AnotherController.php and a function coolAction() inside it, the path would be

yousite.com/helloworld/another/cool

If you are getting 404 errors, make sure you have done all steps correctly. There should not be any extra whitespace in the config.xml. If you did everything correct it will work, because I am creating module along with you. And it works!

Homework

If you got it working, create another module and achieve the same thing again and again until you are clear. If not keep trying.

If you think this tutorial is helping you, share it with your friends on Facebook, Twitter etc 😛 Alright, get ready for the next lesson! Ciao!

The following two tabs change content below.
Hardcore programmer! Been playing with Magento for a while now!

3 thoughts on “Lesson 1: Create a Controller – Magento Extension Tutorial

  1. Hi Hashid,First of all thanks for this regular update about magento module developement.First am not able to create controllers then i tried a lot times then i got the output.Thanks Magento Geek Team

Leave a Reply

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