Pages

Friday, 20 June 2014

Magento Developper’s Guide (Lesson 2) – Create your own controller

This tutorial is the 2nd of many tutorials , you will now learn how to create your own controller in Magento. If you have not read the first articles yet, I strongly advise you to do so.  With Magento, you can access to your controller with the url:yoursite.com/plugin/method/

Start by creating your module:

1 – Create the folder /app/code/local/Easylife/Test/
This create your namespace Easylife and your module Test .

2 – Enable the module in Magento:
Add a file Easylife_all.xml /app/etc/module/

And insert the following code in it

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

This file is used to declare all your modules in the Easylife namespace, you have to put it between the tags « modules » declaration of your plugin one after the other.

<Easylife_Test>
            <active>true</active>
            <codePool>local</codePool>
</Easylife_Test>

Easylife_Test allows you to declare the namespace of your plugin Test Easylife.

 active: true = the plugin is active
false = the plugin is not enabled

codePool: The module will be in the local file because it is a module that we create ourselves (remember the lesson 1).
Magento will therefore find the files in that module in the /app/code/ local/Easylife/Test/

ahead and create the controller:

go to /app/code/local/Easylife/Test/

1 – Create the file /app/code/local/Easylife/Test/controllers/
2 – Inside create a IndexController.php which will contain:

<?php
class Easylife_Test_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction ()
    {
        echo 'test index';
    }
    public function mymethodeAction ()
    {
        echo 'test my method';
    }

}

Now if you go to yoursite.com/test/index

What’s happening?

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

Config.xml file to configure your plugin:

In the same way that you have declared your plugin for Magento to take into account Easylife_All.xml, the file config.xml of your plugin will allow you to you declare the controller of your plugin.

Mangento will check Easylife_All.xml and find your file, it will go in your /app/code/local/Easylife/Test/etc/ and reads the config.xml file to see or pick up your controller.
So create your file Test , a record etc/ with in config.xml .
config.xml contains:

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Test>
            <version>0.1.0</version>
        </Easylife_Test>
    </modules>
    <frontend>
        <routers>
            <test>
                <use>standard</use>
                <args>
                    <module>Easylife_Test</module>
                    <frontName>test</frontName>
                </args>
            </test>
        </routers>
    </frontend>
</config>

Explanation:

 <modules>
        <Easylife_Test>
            <version>0.1.0</version>
        </Easylife_Test>
    </modules>

In this section, we declare the identity of the module for magento module to verify that what you declare in your Easylife_All.xml is the good plugin and if the version number allow you to eventually have an update for your plugin.

<routers>
            <test>
                <use>standard</use>
                <args>
                    <module>Easylife_Test</module>
                    <frontName>test</frontName>
                </args>
            </test>
</routers>

Allows you to declare a router « routeurfrontend » which is actually the road used by magento to access your controller. In our example, the router is « standard » (it means that it is a module that will be displayed on the frontend).The module name is Easylife_Test and the shortcut to access it via the url is test and when you will type yoursite.com/test/index, we arrive on the index method of your controller IndexController.php (in your folder controllers )

You can also access it using your router in place of frontName.

yoursite.com/routeurfrontend/index/

Creating controllers in magento


Now test your module :

yoursite.com/test/index you can read « test index » (you can add /index at the end of your url if you want to call the index method but by default if nothing is specified it calls the method « index »)
You know now create a controller. Now try to understand this by doing it again and again at home and you should be able to move on the next tutorial.

If you have any questions, feel free to leave a comment. If you want to help me, share a link to this website with your friends on facebook, twitter etc…


Find the summery of this tutorial




 

No comments:

Post a Comment