Create Your First Front Controller

In magento we have two types of controllers: front controllers and admin controllers. In this guide we will create very simple front controller with hello world method, than we call it with URL.

You need to create module first, if you don’t know how, follow this tutorial.

In your module folder open [module folder]/etc/config.xml and register your new route like this:

<config>
   <global>
   </global>
   <frontend>
      <routers>
          <[company name]_[module name]> 
              <use>standard</use>
              <args>
                  <module>[Company Name]_[module name]</module>                   
               <frontName>[first_part_of_url]</frontName> <!-- This will be first part of URL --> 
              </args>
          </[company name]_[module name]> 
      </routers> 
   </frontend>
</config>

Create controller file itself

We will call our controller IndexController.php and we will create it inside our module controllers forlder.

so create file like this [module folder]/controllers/IndexController.php and write following code:

<?php
// Watch out, [Company name] and [Module name] has to by capitalized
class [Company name]_[Module name]_IndexController extends Mage_Core_Controller_Front_Action {
    
    public function helloAction () {
        echo 'Hello World.';
    }

    public function indexAction () {
        echo 'This is index action.';
    }

}

Don’t forget to flush cache and then you should be able to call URL like this

http://yourwebsite.com/[module name]/index/hello

you will see Hello world

and when you call this http://yourwebsite.com/[module name]/

you will see This is index action.

If you want to see the store, you need to render shop files. Just add these two lines of code into your controller’s method.

$this->loadLayout(); // wich elements to get?
$this->renderLayout(); // render them

 

 


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.