background top

Extending the Controller Class in CodeIgniter

CodeIgniter is a great little PHP framework that allows you to rapidly build MVC web applications. Without getting too far into Model View Controller ideology, this post will explain how to extend CodeIgniter’s Controller class to achieve site-wide authentication checks.

Not long after starting your fancy new webapp, you will start to realize that putting

if (!isset($_SESSION['isloggedin'])) {
  redirect('/login');
}

in every single controller function is very tedious.

You can create an extension of the Controller class and put the common code in there. Here’s how.

Create a file in /system/application/libraries called MY_Controller.php. The name of this file is important. If the prefix “MY_” doesn’t do it for you, go into /system/application/config/config.php and change

$config['subclass_prefix'] = 'MY_';

to whatever you want. Don’t change the “Controller” part though! You can also extend Models and Helpers.

Start with the following code in MY_Controller.php:

class Authenticated_Controller extends Controller {
  function Authenticated_Controller() {
    parent::Controller();
    session_start();
    if (!isset($_SESSION['isloggedin'])) {
      redirect('/login');
    }
  }
}

Go ahead and add any common controller functions to this class. Feel free to change “Authenticated_Controller” to whatever you want. You can also have multiple Controller extensions in this file.

Note: If you only have one controller extension, it is standard to name your class MY_Controller (rather than Authenticated_Controller).

Important Note: If you find yourself creating a bunch of controller extensions, take a step back and consider what you are doing. Is your extension being used by only one controller? If so, just put your code in that controller’s constructor.

Now it’s time to start using your new controller extension!

In every controller that requires authentication (don’t do this for your login controller!) rather than the traditional

class Manage extends Controller { ...

use this instead:

class Manage extends Authenticated_Controller { ...

Do you have CodeIgniter controller extensions that could be useful to others? Let us know in the comments.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • TwitThis

10 Responses to “Extending the Controller Class in CodeIgniter”

  1. SeanJA Says:

    I do wish that CodeIgniter dropped support for php 4… not having __controller and __call and __autoload really hamper it’s style.

  2. Cole Thorsen Says:

    Interesting way of managing authentication. I set up a library and call the logged in method either in the construct or the individual methods of the controller depending on whether every method requires the user to be logged in or just some.

    Your way definitely cuts down on code which is always nice.

    As a side note do you avoid the codeigniter session class? I know there is a lot of debate over the ci session setup vs. Native PHP sessions.

  3. SeanJA Says:

    Good news! CodeIgniter 2.0 will be phasing out support for PHP 4 (and by 2.1 they say that PHP4 stuff will probably not work at all).

  4. gavinblair Says:

    @Cole – I tend to stick to what most coders are familiar with seeing regardless of framework, which means actual HTML (even forms) in the views, actual SQL queries and $_ variables.

  5. SeanJA Says:

    @gavinblair

    Why oh god why would you type out an entire form when there is a form helper there?

    &ltinput type=text name=name id=id value=<php echo isset($_POST[value])? $_POST[value]:null; ?> />

    vs

    $value = post(‘value’);
    f->input(‘name’, ‘id’, $value);

  6. SeanJA Says:

    And… I almost didn’t mistype some of that stuff ah well… you get the point

  7. SeanJA Says:

    @Cole doesn’t the Code Igniter session class use normal PHP sessions unless you tell it to use the database instead?

  8. Aaron McGowan Says:

    Good post Gavin.

  9. Cole Thorsen Says:

    @gavinblair I tend to make use of standard code for forms, sometimes the codeigniter form helper is nicer though. I do like the $this->input->post(‘item’); purely because I don’t have to check for isset it does it for me. I originally resisted the active record class as well, but I’ve started to take a more serious look at it.

    @SeanJA it is cookie based. http://codeigniter.com/user_guide/libraries/sessions.html

  10. rtraction » Blog Archive » Extending the Controller Class in … | Source code bank Says:

    [...] here to see the original: rtraction » Blog Archive » Extending the Controller Class in … If you enjoyed this article please consider sharing [...]

Leave a Reply