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_ or it’s also called _ . 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/
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.HTML Code:<?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>
Next go the folder app/code/local/ and then create folder structure as shown here
Next we create the configuration file of the module: config.xml inside folder Excellence/Test/etc. So etc/config.xml is created
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.HTML Code:<?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>
Now lets create a file called IndexController.php.
Here are few important things to note:PHP Code:
class Excellence_Test_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
echo "Hello World";
}
}
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
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 CamelCasedView more threads in the same category:
- Prevent an array filter warning while adding products
- Best Development Practices in Magento 2
- How to Secure Magento Against SQL Injections
- How To Backup And Rollback In Magento 2
- Implement Your Custom API In Magento Backend
- How to Create Simple Twitter Feed Module in Magento 2
- How To Secure cron.php In Magento 2
- How To Create & Configure Multi Store In Magento 2
- Create Your Own Widget In Magento
- Third Part of Magento Theme Development
Bookmarks