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:
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.
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:
This file will contain all our declarations and explains the actions of our module.
Put the following code in the config.xml file:
<myrouter> <!-- router name - can be anything -->
<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!
Now lets go to the code section of our module and create our controller like this:
Insert the following code into it:
class Hashid_First_IndexController extends Mage_Core_Controller_Front_Action
public function indexAction()
echo "Yay! My controller is working";
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.
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
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!
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!
Latest posts by Hashid Hameed (see all)
- Magento REST API – Solution for 400 Bad Request - November 12, 2015
- Refund Store Credit Automatically on Order Cancel - October 17, 2014
- Export and Import Product Attributes and Options to CSV - August 5, 2014