Creating a Magento Extension I

Magento uses a MVC (Model-View-Controller) architecture. In very few words, the MVC architecture consists of separating the code responsible for the business logic of an application (model) from its visual representation (view) and the actions that can be performed on that logic (controller).

In this first part of the tutorial, we will create a very simple extension, which will not contain any business logic. Moreover, we will not focus on the view for the moment, since what we want to do is simply to indicate the steps to be taken to create a new module. However, in order to provide a minimum of functionality to our first extension, and so that we can test it and see that it works, we will cover, in a simple way, how controllers work. Models and Views will be covered in the following parts of the tutorial.

Let’s get started!

We need to tell Magento about the existence of our extension. This is done by creating an xml file with the namespace name and the extension name in the /app/etc/modules/ folder (in this tutorial we will assume that / is the path to our Magento installation folder).

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

Once this file is created, and after updating the Magento cache – System -> Cache Management – we will be able to see that Magento has recognised our extension. To do this, go to the System -> Configuration menu in the backstore, and in the menu on the left Advanzed -> Advanzed. Here we will be able to enable or disable the visual output of all installed extensions. If our extension is installed (recognised by Magento) it should be in this list. Regarding the cache, for convenience, I recommend disabling it while we are developing the extension, and re-enabling it when we are done.

List of Extensions in Disable Modules Output shows the new extension Brainsins_Holamundo
Brainsinsins_Holamundo is listed.

Next, we will create the directory for our Extension:

/app/code/local/Brainsins/Holamundo

This directory will store the files of our extension. As we progress through other parts of the tutorial, we will add subdirectories. The Brainsins subdirectory corresponds to the namespace for all our extensions, while the HolaMundo folder is the folder that corresponds directly to the extension. The reason for the namespace is so that our extension HolaMundo can be differentiated from other extensions with the same name, as these will belong to other namespaces.

Inside this directory we will create the following folders:

/app/code/local/Brainsins/Holamundo/etc
/app/code/local/Brainsins/Holamundo/controller

Next, we will create the configuration file for our module in the etc directory.

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

The important part of this file is contained in the <module></module> . With it we specify user interaction elements for our extension. The tag will indicate the drivers that the module will have. The tag will be the url relative to our shop for the controller to be executed. That is to say, if the url of our shop is www.mymagentostore.com, our controller will be executed in the urls that begin with www.mymagentostore.com/holamundo.

We have indicated in the configuration file that we have drivers for the extension, now we have to create a driver:

/app/code/local/Brainsins/Holamundo/controllers/IndexController.php
<?php

class Brainsins_Holamundo_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        echo 'Hello world!';
    }
}

About this file, I want to highlight a few things:

If we look at the name of the class: Brainsinsins_Holamundo_IndexController, we can notice how it has a relationship with the path and the name of the file that contains it:

/app/code/local/Brainsinsins/Holamundo/controllers/IndexController.php.

We will see this behaviour repeated with all the classes we create.
All the classes we create will use the Inheritance mechanism. In this case we inherit from the Mage_Core_Controller_Front_Action class, that’s why our controller code is so simple.
Once this is done, we can observe the behaviour of our extension! To do this, just access the url of our shop by adding /holamundo. For example, www.mymagentostore.com/holamundo. As expected, we will see a blank page with the message “Hello World! We will also see the same message if we enter the addresses www.mymagentostore.com/holamundo/index and www.mymagentostore.com/holamundo/index/index. This is because “index” is both the default controller and the default action, and in Magento, to decide which controller to execute and which method to execute (action) within that controller, the following url structure is used:

www.mymagentostore.com/controller/action

If you notice, the index controller is associated to the IndexController controller and the index action to the indexAction method.

To add another action, just implement a second method, for example, greetAction(), in our controller:

/app/code/local/Brainsins/Holamundo/controllers/IndexController.php
<?php

class Brainsins_Holamundo_IndexController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
        echo 'Hello world!';
    }
    public function saludarAction()
    {
        $input = $this->getRequest()->getParam("nombre");
        $name = $input ? $input : "Unknow";
        echo("Hello " . $name . "!");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *