Changing the default controller naming convention in CodeIgniter

20 September 2009 » In Code Igniter, PHP, Programming, Web Development »

CodeIgniter is one of my favorite framework and I often use it for developing application quickly. Although it is very flexible in most cases, I find its naming convention to be strict. Many times I have faced this problem when my controller’s class name and a model/library’s class names are the same – a Fatal error is inevitable and I have to forcefully change the name of the conflicting class to something less desirable (say from Users to UsersModel). Today I wanted to end this problem.

So I extended the CI_Router core class and made change to the _validate_request method. Now I can name my controller classes in this fashion: UsersController and it resides on the file system as controllers/UsersController.php. If you’ve tried other established frameworks, you should notice that this naming convention is widely used. So, if you have the same need, then just download the MY_Router.php file and put it on your application/libraries folder. That’s it.

Here is how your controller would start:

<?php

class UsersController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}


If you’d like to have a look at the code, here is the source for quick view:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * MY_Router
 *
 * Extended the core CI_Router class in order to force a different naming
 * convention for controllers.
 *
 */
class MY_Router extends CI_Router
{
    /*
     * Suffix in controller name
     *
     * @var String
     */
    private $_suffix = "Controller";

    /*
     * Call the parent constructor
     *
     * This is a requirement for extending base CI core class. Just abiding by
     * the rules.
     *
     * @access  public
     * @return  void
     */
    public function MY_Router()
    {
        parent::CI_Router();
    }

    /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access   private
     * @param    array
     * @return   array
     */
    function _validate_request($segments)
    {
        // Retain the original segments
        $orgSegments = array_slice($segments, 0);

        // Add suffix to the end
        $segments[0] = ucfirst($segments[0]) . $this->_suffix;

        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // OK, revert to the original segment
        $segments[0] = $orgSegments[0];

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0)
            {
                // Add suffix to the end
                $segments[0] = ucfirst($segments[0]) . $this->_suffix;

                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                // Add suffix to the end
                $this->default_controller = ucfirst($this->default_controller) . $this->_suffix;

                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }
}

Cheers!

Share and Enjoy:
  • Digg
  • DZone
  • Twitter
  • Posterous
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Simpy
  • Ping.fm
  • Tumblr

Tags: , , , ,

Trackback URL

5 Comments on "Changing the default controller naming convention in CodeIgniter"

  1. admin
    ariful
    21/09/2009 at 3:17 am Permalink

    very helpful and nice extended class. thanks emran vai

  2. admin
    Md Emran Hasan (phpfour)
    21/09/2009 at 3:30 am Permalink

    You're welcome :)

  3. admin
    Lenin
    21/09/2009 at 4:01 pm Permalink

    Great work :)

  4. admin
    Lenin
    21/09/2009 at 11:01 pm Permalink

    Great work :)

Trackbacks

  1. [...] Hasan has a quick new post to his blog today looking at how you can change the default controller ...

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments