PHP Magento Tips

How to Create Simple Frontend Controller In Magento

Rate this posting:
{[['']]}

With this example, you can easily create a Simple frontend controller which is totally for a learning Purpose.

Step 1:

In app/etc/modules/Foo_Bar.xml add below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Foo_Bar>
      <active>true</active>
      <codePool>local</codePool>
    </Foo_Bar>
  </modules>
</config> 

Step 2:

In app/code/local/Foo/Bar/etc/config.xml add below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Foo_Bar>
            <version>0.1.0</version>
        </Foo_Bar>
    </modules>
    <frontend><!-- | adminhtml -->
    <routers>
        <foo_bar><!--just needs to be unique-->
            <use>standard</use> <!-- admin -->
            <args>
                <module>Foo_Bar</module>
                <frontName>foo</frontName>
            </args>
        </foo_bar>
    </routers>
    </frontend>
</config> 

Here <module> node match our module node in our Registration file.So, if you have any doubt about what to put there then just open main configuration file (app/etc/modules/Foo_Bar.xml Here) and look for text immediately after <modules> node. It is simply a folder path.

Step 3:

In app/code/local/Foo/Bar/controllers/PastaController.php add below code:

<?php

/// app/code/local/Foo/Bar/controllers/PastaController.php
class Foo_Bar_PastaController extends Mage_Core_Controller_Front_Action {
    
    public function sleepAction() {
        echo '<h1> I\'m so tired after eating yummy pasta</h1>';
    }
}

Now open your front end with below url:

http://your_front_end_directory_path/foo/pasta/sleep

e.g. site.com/frontname/controller/action

In our case frontname node is foo, controller is pasta and action is sleep. So, URL will become in this way.
0 Comments On "How to Create Simple Frontend Controller In Magento"

Back To Top