Magento Module Development Series – Part4- Module – Hello world

In this blog post, we will learn how to start magento module development and basic files required setup a module.

This is a basic hello world module, what will do is simple print “helloworld” on a magento page, using our custom module. As we have seen earlier, Magento modules follows, MVC pattern i.e. Model , View and Controllers. So in our module also we will have to create files following the MVC architecture.

Before starting with the module we need to first decide the name of the module, because after creating a module it’s very difficult to change the name of the module.
Module name usually comprises of 2 parts <CompanyName>_<ModuleName> or it’s also called <Namespace>_<Module>. In our case I will take the name of module as Excellence_Test.
First step is to create the folders and files for our module.
First file we will create is Excellence_Test.xml file in location app/etc/modules/

<?xml version="1.0"?>
<config>
    <modules>
        <Excellence_Test>   <!-- Name of Module --> 
            <active>true</active>  <!-- This says if the module is active or not -->
            <codePool>local</codePool> <!-- This says the location of the module i.e inside the local folder. It can also be community folder. -->
        </Excellence_Test>
    </modules>
</config>

This is a very important file, which tells magento about our module. Based on this file only, magento reads our module files. If in some case, you would want to disable the module, you can simple rename this file to some other name.
Next go the folder app/code/local/ and then create folder structure as shown here Magento Module Folder Structure
Next we create the configuration file of the module: config.xml inside folder Excellence/Test/etc. So etc/config.xml is created

<?xml version="1.0"?>
<config>
	<modules>
		<Excellence_Test>
			<version>0.1.0</version>    <!-- Version of module -->
		</Excellence_Test>
	</modules>
	<frontend>
		<routers>
			<test>
				<use>standard</use>
				<args>
					<module>Excellence_Test</module>
					<frontName>test</frontName>  <!-- This is the URL
 of the module. i.e www.yourmagento.com/index.php/test will be the url of your module. -->
				</args>
			</test>
		</routers>
	</frontend>
	<global>
		<blocks>
			<test>
				<class>Excellence_Test_Block</class>  <!-- Path of the
 Block Folder, where all php files are located related to view -->
			</test>
		</blocks>
		<helpers>
			<test>
				<class>Excellence_Test_Helper</class>  
<!-- Path of Helper Files -->
			</test>
		</helpers>
	</global>
</config>

Next, we will create the Controller file. Controller files are used to manage between view and models. Controllers files are the first file to get executed when open a URL. Will explain URL and Controller relationship in detail later.
Now lets create a file called IndexController.php.

class Excellence_Test_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
		echo "Hello World";
    }
}

Here are few important things to note:

  1. First is class name i.e Excellence_Test_IndexController
    In magento, class name depends on the folder location of the file.
    So for example the class Mage_Core_Controller_Front_Action, exists at location Mage/Core/Controller/Front/Action.php
  2. All controller file must extend Mage_Core_Controller_Front_Action to work properly

Ok, now to test if the module work properly open the url www.your-magento.com/test/ this should display Hello World on the screen.

Notes:

  • When creating controller files or block file, etc remember to be careful about case sensitivity of file names. Especially, when you upload your module to linux server the module may stop working, if file cases and class name have not been put in carefully.
  • All class names should be case-sensitive as well. Basically, based on the class name, magento includes that file. So, if class name is not correct, the file is not found by magento.
  • If you’re module beginner, try to keep the module name CamelCased
  • . If your choose module names like Excellence_TestReport this might create a problem. As you can see “TestReport” has two capital letter, so this sometimes creates the problem in linux server.

  • It’s general good practice to keep all public functions in magento as CamelCased
  • Michael

    You should specify that IndexController.php goes in the Test/controllers directory.  You sort of imply that the path should be Excellence/Test/IndexController.php by giving the example of the path for Mage_Core_Controller_Front_Action being Mage/Core/Controller/Front/Action.php.

    • Manish Prakash

      Sorry, couldn’t understand what you mean.

  • DrZ

    Hi, 
    I’m getting this output:ForbiddenYou do not have permission to access this document. 

    Any ideas?
    Thankyou

    • Manish Prakash

      Where do you see this

  • http://www.ecreations.mu/ Youshrin

    hi..i am always getting error 404 for any module i am creating…is there a solution?
    can anyone help please?

  • Rajan

    thanks

  • gaurav

    i have downloaded latest version of magento :

    file structure there is

    magento/app/code/(core& community)

    not as magento/app/code/local

    • http://www.facebook.com/vasile.vetisan Vasile Vetisan

      U must create local folder, and in local folder U must create controllers, models, etc, sql, Block and all the rest of them

  • http://www.emsnewbie.com/ Ron Davis

    You may need to flush your cache. config.xml is one of the files cached. I just turned them all off while developing.

  • http://www.emsnewbie.com/ Ron Davis

    Nice tutorial. Couple of minor things that will bite you.

    If you copy the code for index controller out of the post, you need to add at the end.

    I couldn’t get magento/test to work, but magento/index.php/test does work. This may have to do with my installed configuration.

  • Nicolas

    It did exactly all you did but It does not work. Please Help. The URL http://www.your-magento.com/test/ I end up on a page “NOT FOUND”. When I use http://www.your-magento.com/index.php/test/ I end up on the wamp server page but no “Hello world”. Please Help

  • Ravi Patel

    I tried to create hello world module. i have follow all the above all steps. but all time i have get 404 Not Found occured

  • Sandip Shah

    OK. I tried everything and it did not work. I had to make two changes:

    put the IndexController.php file in Excellence/Test/ folder.

    And the IndexController.php file is missing <?php at the top

    After making these two changes, both the urls work

    http://mydomain.com/test
    and
    http://mydomain.com/index.php/test

    Thanks for the Tutorial series.

    SS

    • Manish Prakash

      You need to place the IndexController.php inside the “controllers” folder.
      Then it will work :)

  • Vetrivel

    Hi, I tried to follow above ways, it works perfectly….
    Thanks.

  • Yogesh Patel

    Hi, i also have the same problem but when i tried by adding “index.php” in url, it works perfectly. thanks

  • Bhadresh siddhapara

    It’s work perfectly in magento 1.8.0

  • Rehan

    Hi, I tried to follows the steps exactly. but its always gives me 404 Not Found

    • Rehan

      Now it’s working when clear cache..but let me know one thing how can i assign template for this page because it’s displaying on blank page.?