ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Custom Base Classes (Controllers) (CI 2.0)

November 18, 2010 3:13am

Subscribe [6]
  • #1 / Nov 18, 2010 3:13am

    RandyCram

    19 posts

    Most code I create and that I have seen built on CodeIgniter use custom base controllers but do not always start their names with “MY.”

    I like to use things such as:
    Public_Controller.php and Admin_Controller.php

    In CodeIgniter 2.0 they move custom base classes (controllers) from the “applications/libraries” folder to the “applications/core” folder.

    If you followed Phil Sturgeon’s guide on using custom base classes then you know you need to add an auto-load to the config.php file. Hover the code he shares still tries to load the files from the “libraries” folder. I have changed the code to try and load them from the “core” folder and I wanted to share it with you.

    /*
    | -------------------------------------------------------------------
    |  Native Auto-load
    | -------------------------------------------------------------------
    | 
    | Nothing to do with config/autoload.php, this allows PHP autoload to work
    | for base controllers and some third-party libraries.
    |
    */
    function __autoload($class)
    {
     if(strpos($class, 'CI_') !== 0)
     {
      @include_once( APPPATH . 'core/'. $class . EXT );
     }
    }

    Simply stick that at the bottom of your application/config.php file and your good to go for CI2.0

    Note: if you already have it defined be sure to remove the current one or you will have an error.

    Cheers,
    Randy Cram

  • #2 / Nov 18, 2010 5:41am

    Phil Sturgeon

    2889 posts

    Good point, even better:

    function __autoload($class)
    {
        if (strpos($class, 'CI_') !== 0)
        {
            if (file_exists($file = APPPATH . 'core/' . $class . EXT))
            {
                include $file;
            }
    
            else if (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
            {
                include $file;
            }
        }
    }
  • #3 / Nov 18, 2010 6:55pm

    RandyCram

    19 posts

    Ah, Good thinking there Phil!

  • #4 / Nov 20, 2010 2:49am

    echoDreamz

    77 posts

    function __autoload($class)
    {
        static $objects = array();
        
        // Make sure this class has not already been loaded or is not a CI base class
        if (preg_match('{^CI_}', $class) || in_array($class, $objects))
        {
            log_message('debug', "Hooks: Skipped auto loading class {$class}");
            return;
        }
        
        // Attempt to load the requested class
        $class_path = APPPATH . "libraries/{$class}" . EXT;
        if (file_exists($class_path))
        {
            include($class_path);
            $objects[] = $class;
            log_message('debug', "Hooks: Autoloaded Class {$class}.");
        }
        else
        {
            log_message('error', "Hooks: Failed auto loading class {$class} from path {$class_path}.");
        }
    }

    What I use.

  • #5 / Dec 08, 2010 2:07pm

    cyrilkas

    3 posts

    I’m newbie with PHP OOP stuffs.
    But my code work only if i wrote this:

    class MY_Controller extends CI_Controller
    {        
        function __construct()
        {
            parent::__construct();
        }
    }

    Is it correct? What is the best practice?

    Thanks…

  • #6 / Dec 08, 2010 4:44pm

    Devon Lambert

    139 posts

    I’m newbie with PHP OOP stuffs.
    But my code work only if i wrote this:

    class MY_Controller extends CI_Controller
    {        
        function __construct()
        {
            parent::__construct();
        }
    }

    Is it correct? What is the best practice?

    Thanks…

    Hey Cyrilkas,

    What the gang is talking about here is the autoloading of Custom/Core Controllers. What you are referring to is the code that will be used in those controllers. However, your code is correct and especially true if you are using CI 2.0 as it is now entirely based on PHP 5.

  • #7 / Dec 09, 2010 5:58am

    cyrilkas

    3 posts

    Thanks Devon, i realize that later!

  • #8 / Mar 22, 2011 5:16pm

    Kevin Smith

    4784 posts

    I feel like I’m missing something here. I followed Phil’s guide on keeping it DRY, and it worked before I had to add the autoloading bit of code to the end of the config.php file. Was autoloading from the ./application/core directory added to CI2 Reactor since this guide was written?

  • #9 / Apr 19, 2011 11:59am

    renathy

    4 posts

    I want to create database driven navigation menu from data base. I have created library that mainly contains menu class and show_menu method.
    I do not want to load this library in every controller for my page.
    Can I use base controller idea (i have read Phil Sturgeon’s guide) to do this?
    I have already tried to create base controller.

    R.

  • #10 / Apr 19, 2011 4:17pm

    InsiteFX

    6819 posts

    Why not autoload the your library in application/config/autoload.php ?

    InsiteFX

  • #11 / Apr 19, 2011 4:42pm

    renathy

    4 posts

    I have solved my problem…
    The problem was not to load library in base controler or to autoload.
    The cause of my problem was that I didn’t know that $data variable is somehow “globaly” seen in base controle and in or controlers extended from base controler (say MyPage extended BaseController).
    So I didn’t know that if writing $data[‘menu’] = generete_menu() in base controller I can use this $data[‘menu’] in my MyPage controller.

    R.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases