CodeIgniter Setup – Part 2

Here I will explain how to setup Base Contollers in CodeIgniter.

This follows on from Part 1 so I recommend that you read this first before continuing.

Full working example of all 3 parts available on GitHub:
https://github.com/glynrob/CodeIgniter-Setup

If you only want the code for Part 2 you can pull Stage 2 from the repo.

Why would you want to use Base Contollers?

Base Contollers allow you to assign any required functionality to groups of controllers that call them.
My example uses My_Contoller which is called by all contollers.
So if I wanted to disable the whole site, all I would have to do is include a holding page in this contoller and then die so nothing else runs. This saves me having to go through each contoller to disable the site.
My example on GitHub also creates a CMS_Contoller which all CMS contollers will call. In this CMS_Contoller it checks the user is logged in. If they are not them they are redirected to a login page.
This is usful so you don’t forget to put these checks in a controller opening you up for abuse from non-registered users.

edit core/application/config/config.php

$config['encryption_key'] = '123456'; // something hard to gues

add to the bottom
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'core/'. $class . EXT );
}
}

edit core/application/config/autoload.php
$autoload['libraries'] = array('session'); // for session data
$autoload['helper'] = array('url'); // for redirects

Add  core/application/core/My_Contoller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller{

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

edit core/application/controllers/welcome.php
class Welcome extends MY_Controller {

Look at my example on GitHub on creating the CMS_Contoller and checking if a user is logged in.

So this how you setup Base Contollers.
Part 3 will show you how to create Base Models.

4 comments for “CodeIgniter Setup – Part 2

  1. Tony
    October 4, 2012 at 2:40 pm

    This is a nice set up, thank you for sharing it. I’d like to ask you: How do links get resolved when the application is no longer in the site folder? For instance the link on the welcome page Click here to view CMS.

    I’ve been trying to set up my own site with the application outside of the site folder and found that the class/method/id/ URI method fails – it can’t route the request because it doesn’t see the controller folder.

  2. GlynRob
    October 25, 2012 at 1:49 pm

    Hi Tony,
    The issue I expect is due to the pages not hitting /www/index.php
    If you add a .htaccess at /www/.htaccess everything should work.

    Example

  3. mafia
    February 4, 2013 at 3:43 am

    I tried your setup, but did not move the System and Application to core.Inside my application/core folder, I have created a MY_Controller extending CI_Controller, and auth_controller extending MY_Controller. However, when i used the auth_controller to be extended from my controller page, a fatal error occured: “Class ‘auth_controller’ not found”

  4. GlynRob
    February 9, 2013 at 7:51 pm

    Hi,
    If you try MY_Controller and you don’t get an error then I would say the issue would be capital letters not matching. (This is case sensitive)
    Check spelling of course.
    Same for the file name and the class name of the core controller.
    If this doesn’t work then it is hard to help without the code. Is it on github by anychange?

    If you try MY_Controller and you get a similar error then the issue would be due to further back in the setup procedure.

    Hope this is of some help.

Leave a Reply

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