Lesson 1 - Guiding to create the first module

09 Jul 2014
...

Hey guys, I’m Brian To, I’m  a senior developer at  Mage-World.Com

You know, for the first time when I was a beginner who just got acquaintance and develop extensions of Magento, I had to learn and find as much information as I could to create the first module by myself.

For my real experiences, I will try to make sense for you all the knowledge I got by the easiest way.

TOOL & VERSION

-          In this writing, I use Magento version 1.8.1 (the information can be applied for version 1.6 to 1.9)

-          Editor phpStorm 7.x

The FIRST lesson before creating an extension is: DISABLE THE CACHE.

DISABLE THE CACHE

You can do this by going to Admin Panel > System > Cache Management > Select All > Actions: Disable > Submit 

YOU CAN WATCH THIS VIDEO TO SEE:

CONSTRUCTION OF MODULE – EXTENSION

In general, each module has 2 parts: Code – Template

Code ( contains all controllers, model, SQL… to settle users activities)

for example:

Explanation:

- As you see Code of the module is in: app/code/local/. Ở đây:

+ YourNamespace: is understood as the company’s name or name of the developer.

+ YourModulename: is the name of the module that you are developing, you should set a short, easy to understand and clearly name

Therefore, in this case, we can take an example of naming for a module like this:

* Namespace: MW (stands for Mage-World).

* Modulename: Freegift

*The full link for the Freegift module is: app/code/local/MW/Freegift

 (Anyone confusing why it is not the code of Module in community or core?)

- Template (we will find out later).

TWO STEPS FOR CREATING A MODULE

We will create a simple module first, which named: SimpleNews

- Module name:  SimpleNews

- Namespace:  MW

Step  1:  Create folder: app/code/local/MW/SimpleNews

Step  2:  Create file: app/etc/modules/MW_SimpleNews.xml

And insert this code to file MW_SimpleNews.xml:

<?xml version="1.0"?>
<config>
    <modules> 
        <MW_SimpleNews>
            <active>true</active>
            <codePool>local</codePool>
        </MW_SimpleNews>
    </modules>
</config>

Explanation:

- File MW_SimpleNews.xml  used to configure global module. You only need to copy&paste in this part. If you want for more details, you can go to this link:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/module_config.xml

Finally, you go here: Admin Panel  > System > Advanced > Advanced if you see MW_SimpleNews in Disable Modules Output the module starts.

YOU CAN WATCH THIS VIDEO TO SEE:

 

CREATE THE CONTROLLER

As I mentioned in “CONSTRUCTION OF MODULE – EXTENSION”, all the codes of the module will be in app/code/local/{Namespace}/{Modulename}

We made the module SimpleNews. Now, we continue making this file:

app/code/local/MW/SimpleNews/controllers/IndexController.php

And insert this code to file:

class MW_SimpleNews_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo 'Hello World.';
    }
}

Explanation:

- Class name MW_SimpleNews_IndexController divided into 2 parts:

+ MW_SimpleNews: You can see again in this file: app/code/local/MW/SimpleNews/etc/config.xml tag <modules >

+ IndexController: This is the file name of controller

- Mage_Core_Controller_Front_Action: Each controller starts in frontend will extend from this class. You only need to Remember, Copy & Paste.

And now if you go to yoursite.com/index.php/news/index what happening?

Actually … you can not find the page. There’s an error because you have not already declared your controller in the plugin.

CONFIG YOUR MODULE IN CONFIG.XML

The file config.xml of your plugin will allow to declare the controller of your plugin.

Create file: app/code/local/MW/SimpleNews/etc/config.xml

And insert this code to file:

<?xml version="1.0"?>
<config>
    <modules>
        <MW_SimpleNews>
            <version>1.0.0</version>
        </MW_SimpleNews>
    </modules>
    <frontend>
        <routers>
            <simplenews_router>
                <use>standard</use>
                <args>
                    <module>MW_SimpleNews</module>
                    <frontName>news</frontName>
                </args>
            </simplenews_router>
        </routers>
    </frontend>
</config>

Explanation:

<modules>
    <MW_SimpleNews>
        <version>1.0.0</version>
    </MW_SimpleNews>
</modules>

In this part, we inform the indentity of the module for Magento to verify what you informed in app/etc/modules/MW_SimpleNews.xml  is good. The version number will allow you to update the latest module.

    
<frontend>
    <routers>
        <simplenews_router>
            <use>standard</use>
            <args>
                <module>MW_SimpleNews</module>
                <frontName>news</frontName>
            </args>
        </simplenews_router>
    </routers>
</frontend>

In this section you only need to copy&paste but note that:

- Tag  < simplenews_router> you can set any name for easy to see.

- Tag  <module> you must fill exactly the module name you informed in the <modules> above.

- Tag <frontName> this tag allow you identify the router call at frontend, ie:

yoursite.com/index.php/news/index

RUN YOUR MODULE

Open any browser, call URL: yoursite.com/index.php/news/index

You will see: Hello World! 

ADVANCED TUTORIAL

Are you confusing about the param index in URL: /index.php/news/index ?

Index is the file app/code/local/MW/SimpleNews/controllers/IndexController.php

Therefore, how we call the indexAction in the file indexController.php ?

public function indexAction(){
        echo 'Hello World.';
}

They will be called by this full URL:

yoursite.com/index.php/news/index/index

index:  This param will be called to the indexAction in the file IndexController.php

So, if you call this URL:

yoursite.com/index.php/news/index/abcxyz

It will run to the file:

app/code/local/MW/SimpleNews/controllers/IndexController.php and find abcxyzAction to make it, if not, it will appear 404 Not found

Hence, we will write one more abcxyzAction into it, the file IndexController.php will be:

class MW_SimpleNews_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        echo 'Hello World.';
    }
    public function abcxyzAction(){
        echo 'Test another method';
    }
}

Open any browser, call URL: yoursite.com/index.php/news/index/abcxyz

You will see: Test another method! 

YOU CAN WATCH THIS VIDEO TO SEE: