Here are a few notes on creating your own controllers and actions in Magento modules. This example shows how to create a controller called 'subscriber' for a module called 'MailingList'.

One scenario in which a custom controller is useful is when there is a need to allow other applications to interact with Magento. For example, we might want to be able to take action on subscribers being added or removed from an external mailing list, which is in fact exactly what our Campaign Monitor extension does.

  1. Add a <frontend> section to your module's etc/config.xml file:

    <config>
     ...
     
     <frontend>
     <routers>
     <mailinglist>
     <use>standard</use>
     <args>
     <module>MailingList</module>
     <frontName>mailing</frontName>
     </args>
     </mailinglist>
     </routers>
     </frontend>
     </config>
    

    The contents of <frontName> is the first part of the URL for accessing any controllers in the module.

  2. Create a 'controllers' directory underneath your module's directory.

  3. To actually create a controller, you can take a look at the existing controllers in Magento core for examples. A basic implementation would look something like this:

    class MailingList_SubscriberController extends Mage_Core_Controller_Front_Action
     {
     public function indexAction()
     {
     ...
     }
    
     public function newAction()
     {
     ...
     }
     }
    

    The code above would be the contents of a file called 'SubscriberController.php'. The name of the controller will be the second component of the URL used to access it ('subscriber', in this case).

  4. The indexAction() function will now be called from the URL http://[your magento address]/mailing/subscriber, as this is the default action, so to run the newAction() function, the URL would be http://[your magento address]/mailing/subscriber/new. We can see that the components of the URL are the module's 'front name', controller name, and then action name. Access any URL parameters (the standard '?blah=foo&cat=yes' variety) using the traditional PHP $_GET and $_POST superglobals.