CodeIgniter core classes

One of the things I liked about codeigniter is it lets you create and extend core classes. For example, say I want to extend the functionality of the controller class so that it does not allow me to access certain pages unless I am logged in:

Simply create application/core/MY_Controller.php to extend the core controller class. Then you can do this:

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

   class MY_Controller extends CI_Controller {
    protected $user;

    public function __construct() {
        parent::__construct();

        $this->load->helper('url');
        $this->load->library('session');

        $this->user     = $this->session->userdata('user');
        # or your own way to check if the user is logged in
        $page           = uri_string();

        if (
            !$this->user
            && strpos ( $page, 'login' ) === false
            && strpos ( $page, 'register' ) === false
            && $page != ''
        ) { # this allows you to access pages matching 'login', 'register',
            # and the index page.
            # you can configure this of course.
            $this->session->set_flashdata(
                'error',
                'You must log in to see this page.'
            );

            redirect ('/login');
        }
    }
}
?>

Now when you write your own controller you would extend MY_Controller instead of CI_Controller, for example:

<?php
class Profile extends MY_Controller {

And all your pages will have this functionality. As an added bonus, every controller that extends MY_Controller will have the user session in $this->user.

Another cool thing you can do by extending core controllers is writing functions you want every controller to have available:

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

   class MY_Controller extends CI_Controller {
    protected $user;

    public function __construct() {
        parent::__construct();

        $this->load->helper('url');
        $this->load->library('session');

        $this->user     = $this->session->userdata('user');
        # or your own way to check if the user is logged in
        $page           = uri_string();

        if (
            !$this->user
            && strpos ( $page, 'login' ) === false
            && strpos ( $page, 'register' ) === false
            && $page != ''
        ) { # this allows you to access pages matching 'login', 'register',
            # and the index page.
            # you can configure this of course.
            $this->session->set_flashdata(
                'error',
                'You must log in to see this page.'
            );

            redirect ('/login');
        }
    }

    protected function stash_input() {
        foreach ($this->input->post() as $key => $value) {
            $this->session->set_flashdata($key, $value);
        }
    }
}
?>

stash_input here, for example, will store all of $_POST in your flash data.
this is useful for postback forms.
For example, if an error happens when submitting the form, you can store the input in the session data and then use it in your view:

//...
if ($error) {
$this->stash_input();
$this->session->set_flashdata("error", $error);
redirect(site_url('/your_form'));
return;
}
//...

Overall, extending core classes can be very helpful and is one of my favourite features of codeigniter!

That’s all for now. Until next time, see ya!

2 thoughts on “CodeIgniter core classes

  1. Hi Errietta,

    first of all thanks for this nice blog. i want to know that can we protect model to acess another controllers, means bind the specific model to the particular controller only that controller can use its model.

    please reply

Leave a Comment

Your email address will not be published. Required fields are marked *