SpinOneSolutions | I waste my time so that you don’t have to…

Oct/09

7

Magento – Part III : Controllers

The Problem:

I need to write Part III of the Magento series.

The Solutions:

This article… TADA!

First, let’s talk about the controller.  The default controller for a module is always named IndexController.php.  If you haven’t requested a specific controller (in the URL), than the system is going to look for IndexController.php and load it.  Similarly, the default Magento action is indexAction().

Applying this logic to my custom Module; if I simply type {base}/spinonesolutions/ into the address bar, Magento is going to load up Spinonesolutions_Helloworld_IndexController and invoke indexAction().  Remember that “spinonesolutions” is the frontname that I defined for this module in the config.xml (See Part II : Models for source).

<?php
//IndexController is the default controller
//EG: http://localhost/spinonesolutions/
//Notice there's no parameters being passed as a parameter (Nothing after trailing "/")
//IndexController will be called since it's the default.
//"spinonesolutions" is the frontname as defined in confg.xml
class Spinonesolutions_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
      //indexAction is the default Action for any controller
      function indexAction() {
            echo "indexAction";
            $helloworld = Mage::getModel("spinonesolutions_helloworld/helloworld");
            $helloworld->helloworld("helloworld");
      }
}
<?xml version="1.0" encoding="UTF-8" ?>
<config>
      <modules>
            <Spinonesolutions_Helloworld>
                  <version>0.1.0</version>
            </Spinonesolutions_Helloworld>
      </modules>
    <frontend>
        <routers>
            <spinonesolutions>      <!-- I make this match my front name but I'm not sure it matters -->
                <use>standard</use> <!-- Use standard routing as opposed to admin.  IE: frontend vs admin -->
                <args>
                    <module>Spinonesolutions_Helloworld</module>  <!-- The module to search for controllers -->
                    <frontName>spinonesolutions</frontName> <!-- The first descriminator in the path.  "spinonesolutions" in http://localhost/spinonesolutions/ -->
                </args>
            </spinonesolutions>
        </routers>
    </frontend>
      <global>
            <models>
                  <spinonesolutions_helloworld> <!-- This is used when istanting your Model EG: Mage::getModel("spinonesolutions_helloworld/hellworld") -->
                        <class>Spinonesolutions_Helloworld_Model</class>      <!-- That class to use when isntanting objects of type above. -->
                  </spinonesolutions_helloworld>
            </models>
      </global>
</config>

Let’s start adding pieces onto our URL and explore what happens in Magento.  Adding one piece yields {base}/spinonesolutions/foo/.  This is asking Magento to invoke the indexAction() of the FooController, more specifically Spinonesolutions_Helloworld_FooController.

That Class doesn’t exist in our current code base so we need to create it.  Following Magento’s naming conventions and our choices in config.xml the filename and path for Spinonesolutions_Helloworld_FooController must be: /Spinonesolutions/Helloworld/controllers/FooController.php.

<?php
class Spinonesolutions_Helloworld_FooController extends Mage_Core_Controller_Front_Action {
      //indexAction is the default Action for any controller
      function indexAction() {
            echo "Spinonesolutions_Helloworld_FooController.indexAction()";
      }    
}

Now we’ll take it to the next level and add another piece to the URL yielding {base}/spinonesolutions/foo/bar/.  This is asking Magento to invoke the barAction() of the FooController (Spinonesolutions_Helloworld_FooController). Let’s add that action to our growing controller.

<?php
class Spinonesolutions_Helloworld_FooController extends Mage_Core_Controller_Front_Action {
      //indexAction is the default Action for any controller
      function indexAction() {
            echo "Spinonesolutions_Helloworld_FooController.indexAction()";
      }

      //{base}/spinonesolutions/bar/
      function barAction() {
            echo "Spinonesolutions_Helloworld_FooController.barAction()";
      }
}

That covers the basics for controllers.  Setup your route in the config.xml then follow the proper naming conventions when creating your classes and you should be all set.

Download the source – Helloworld_v2

  1. Part I : Custom Module
  2. Part II : Models
  3. Part III : Controllers
  4. Part IV : Extending Models
  5. Part V : Data Layer
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Kirtsy
  • Propeller
  • Reddit
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati

RSS Feed

1 Comment for Magento – Part III : Controllers

jayendra | November 28, 2011 at 4:16 am

Hi,

Thanks. Its very good article.

Leave a comment!

«

»

Find it!

Theme Design by devolux.org