Adding new action into controller in Magento

Sometimes you have to change the default functionality of Magento, either by modifying the core module (which is not recommended) or by writing your own module. The latter is described in many other websites, so I won’t write about that. But I feel important to write about such a simple thing like adding a new action into a controller.

Why? Because I’m just learning Magento and I have already spent too much time figuring out why it doesn’t work.

As many of you already know, Magento is based on Zend Framework. The basic mechanism of processing an URL is the following:


<protocol> is for example http

<host> is your domain name ex.

<controller> is the name of your controller

<action> is the name of your action

The rule is simple, the controller’s filename is <controller>Controller.php and the action has to be a function inside the controller with the name <action>Action.

Here’s an example:

and it will call the worldAction in helloController.php.

These are the basics you need to know when starting to work with Magento. It is a little bit different but not so much.

The Magento team have made their system ultra-extra extensible, and that’s why it is so slow – at least the community edition. That is the reason why my local apache crashed many times while I tried  to debug with xdebug to understand how Magento works; and beside this, sometimes even Eclipse or Netbeans cannot resolve the correct class names.

I won’t go into the details of routings or other special stuff, I know there are plenty of ways to create a new action, but sometimes it is better to know the basics. In the case of Magento you will see something similar to what we have for Zend Framework:

<protocol>://<host>/[index.php/]<module frontName>/<controller>/<action>[/<parameters>]


The basic logic is the same: you will find the worldAction in the helloController.php in your module’s folder.

Up to this point everything seemed to be clear for me. I opened the browser, entered the url and it redirected me to the home page. Tried to do things like

class ...._HelloController...
  public function worldAction()
    echo "something";

but the “something” didn’t show up. I made a little research to make it work, tried different configuration changes in config.xml but it didn’t helped. I got a tip to make a route in the configuration, which would point a URL to a specific method of a class… what??! Does it seems complicated only for me? I said there must be an easier solution for this.

And the solution is really easy. You may have already observed that on every page (of the admin backend) you can find some junk parameters like …/key/0123456789abcdef0123456789abcdef/. Well, I thought that it is rubbish… it is not.

Suddenly, I said `Let me try to generate the URL to my page within an existing template with something like:

$this->getUrl("*/hello/world", array("myparam1" => "myvalue1"))

which resulted in a bulky, sausage-like  link:

then I opened it in the browser and tadamm!: it showed the “something”.

Briefly: you don’t have to modify your configurations, just simply add your action method into the controller (ex. worldAction into helloController.php) and access your page with the complete URL (which contains the […]/key/[…]/); you can generate the link with the getUrl() function.

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.