Magento 1. Creating a module. Part 3. Create a controller and output Hello Word on the frontend

June 18, 2017 10 Yehor Rykhnov

In this article, we'll create a simple controller, then you can follow the link to see the text Hello World.

Create the controller of our module

We will create a controller that will output plain text to the frontend. Go to our controller using the link "articles"

Create a folder:

app/code/local/My/Articles/controllers

Create a file:

app/code/local/My/Articles/controllers/IndexController.php

Adding the code:

<?php
class My_Articles_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "<h1>Hello World</h1>";
    }
}

Adding parameters to the file:

app/code/local/My/Articles/etc/config.xml

Here we add the following parameters (see comments)

Code for the frontend:

<?xml version="1.0" ?>
<config>
   <modules>
       <My_Articles>
           <version>1.0.0</version>
       </My_Articles>
   </modules>
   <!-- Code for the frontend --> 
   <frontend>
       <routers>
           <marticles>
               <use>standard</use>
               <args>
                   <module>My_Articles</module>
                   <frontName>articles</frontName>
               </args>
           </marticles>
       </routers>
   </frontend>
</config>

frontName - specifies the url to which our controller will respond. It is better not to use standard or existing names.

Follow the link http://my-magento/articles or if several languages are used http://my-magento/en/articles

Must see the text: Hello World

Everything, the controller in the module is ready and working.