How to Setup a Controller/Router to Respond to a Specific URL

Magento uses an EAV database architecture. Whenever possible, try to use the model objects the system provides to get the information you need.

Create A Module
All additions and customizations to Magento are done through modules. So, the first thing you’ll need to do is create a new module. Create an XML file in app/modules named as follows:

Code:

cd /path/to/store/app
touch etc/modules/YourCompanyName_HelloWorld.xml

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

YourCompanyName is a unique namespace for your modifications, it doesn’t have to be your company’s name, but that is the recommended convention of magento. HelloWorld is the name of your module.

Clear the Application Cache.
Now that the module file is in place, we’ll need to let Magento know about it (and check our work). In the admin application:
-- Go to System->Cache Management
-- Select Refresh from the All Cache menu
-- Click Save Cache settings

Now, we make sure that Magento knows about the module:
-- Go to System->Configuration;
-- Click Advanced

In the "Disable modules output" setting box, look for your new module named "YourCompanyName_HelloWorld." If you can live with the performance slow down, you might want to turn off the application cache while developing/learning. Nothing is worse then forgetting the clear out the cache and then your changes aren’t showing up!

Setup the Directory Structure
Setup a directory structure for the module. You won’t need all these directories, but set them all up now.

Code:

mkdir -p app/code/local/MyCompanyName/HelloWorld
mkdir -p app/code/local/YourCompanyName/HelloWorld/Block
mkdir -p app/code/local/YourCompanyName/HelloWorld/controllers
mkdir -p app/code/local/YourCompanyName/HelloWorld/Model
mkdir -p app/code/local/YourCompanyName/HelloWorld/Helper
mkdir -p app/code/local/YourCompanyName/HelloWorld/etc
mkdir -p app/code/local/YourCompanyName/HelloWorld/sql

Add a configuration file

Code:

<?xml version="1.0"?>
<config>
<modules>
<yourcompanyname_helloworld>
<version>
0.1.0
</version>
</yourcompanyname_helloworld>
</modules>
</config>

This configuration file will let you tell Magento what code you want to run!

Setting Up the Router
Now setup the module’s routers. This will let the system know that we’re handling any URLs in the form of
http://example.com/magento/index.php/helloworld

In your configuration file, add the following section:

Code:

<config>
<!-- ... -->
<frontend>
<routers>
<!-- the <helloworld> tagname appears to be arbitrary, but it should match the frontName tag below-->
<helloworld>
<use>standard</use>
<args>
<module>YourCompanyName_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
<!-- ... -->
</config>

What you’re saying here is "any URL with the frontName of helloworld, then this:
http://example.com/magento/index.php/helloworld should use the frontName controller YourCompanyName_HelloWorld".

So, with the above configuration in place, when you load the helloworld page above, you’ll get a 404 page. That’s because you haven’t created a file for your controller.

Create File for Controller

Code:

touch app/code/local/YourCompanyName/HelloWorld/controllers/IndexController.php

Now try loading the page! If you get a 404, go back, start again!

At this point you’ll get a PHP/Magento exception . . . That’s OK !!

It will be as this --> "Controller file was loaded but class does not exist"

Open the file you just created, and paste in the following code. (The name of the class needs to be based on the name you provided in your router.)

Code:

class YourCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action{ public function indexAction(){
        echo "We're echoing just to show that this is what's called, normally you'd have some kind of redirect going on here"; } }

You just setup the module/frontName controller. This is NOT the MVC controller. Try the URL, and you’ll get a 404, even if you had a fooAction method in YourCompanyName_HelloWorld_IndexController.

To setup the MVC controller, create a new file, and enter the following code:

Code:

touch app/code/local/YourCompanyName/HelloWorld/controllers/FooController.php
class YourCompanyName_HelloWorld_FooController extends Mage_Core_Controller_Front_Action{ public function indexAction(){
 echo 'Foo Index Action';  }
 public function addAction(){ echo 'Foo add Action';  }
 public function deleteAction(){ echo 'Foo delete Action';  } }

You should now be able to hit the URL and see the results of your echo statements.

That should give you a basic idea on how Magento dispatches to a controller.

This question is now closed

Written By

Comments