CodeIgniter Forums

Full Version: Loading libraries in subfolder and reflecting the path in $this
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Henrik Pejer[/eluser]
Yeah, I know, you are thinking, 'what the hell is he talking about?'. Let me try to explain, ok? I really like the idea that you could arrange you libraries in folders and sub folders. Currently, it would seem, CI will not let you do this, unless you modify some classes. And this is my attempt.

I also wanted to have the CI-object, or $this, to reflect the folder structure from where the library was loaded.

This is the folder structure, and file, we want to load:

application/libraries/core/page.php

This is how we would load it:

Code:
$this->load->library('core/page.php');

And this is how we would call or 'use' our page class:

Code:
$this->core->page->whatever();

I feel it reflects the structure better.

The code below has not been tested much, but I'd like to hear your take on this. Is it a good idea? What kind of checks do you think would be necessary for this not to be too insecure, or dangerous? I've put comments in the file stating where I've made my changes. If you use it and it works: let me know! If its utter crap, dangerous or generally a very bad idea, tell it to the forum so that others don't feel tempted to use it.

Happy CI-ing!

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

/*
* Class to extend the 1.5.3 Loader class of CodeIgniter
*
* The class will enable you to load libraries in sub-folders
* also the folderstructure will be reflected in the 'path'
* to the object
*
* Example:
* $this->load->library('core/page');
*
* will result in this:
* $this->core->page;
*
* The code below is copied from the original Loader-class in CI
* and where there are <PEJER_MOD> - tags, thats where I've
* done my additions and alterations
*
* by Henrik Pejer
* NOT FULLY TESTED, might lead to strange results....
*/


class MY_Loader extends CI_Loader {



    function MY_Loader(){
        parent::CI_Loader();
    }
    
    

    function _ci_init_class($class, $prefix = '', $config = FALSE)

    {    

        // Is there an associated config file for this class?

        if ($config === NULL)

        {

            $config = NULL;

            if (file_exists(APPPATH.'config/'.$class.EXT))

            {

                include(APPPATH.'config/'.$class.EXT);

            }

        }
        ## <PEJER_MOD> ##

        $origClass=$class;
        $class=basename($class);
        ## </PEJER_MOD> ##

        if ($prefix == '')

        {

            $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;

        }

        else

        {

            $name = $prefix.$class;

        }

        

        // Set the variable name we will assign the class to

        $class = strtolower($class);            

        $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];                


        ## <PEJER_MOD> ##
        $CI =& get_instance();
        if(strstr($origClass,'/')){            
            $path=array_slice(split('/',$origClass),0,-1);
            foreach($path as $key=>$name){
                if(!isset($CI->$name)){
                    $CI->$name=(object)array();
                }
                $CI=&$CI->$name;                
            }
            $name=basename($origClass);
        }
        ## </PEJER_MOD>##
        // Instantiate the class    
        if ($config !== NULL)

        {

            $CI->$classvar = new $name($config);

        }

        else

        {        

            $CI->$classvar = new $name;

        }    

    }    

}

?&gt;

El Forum

[eluser]aroman[/eluser]
nice work pejer ,, its a good habit to organize our classes with subfolders on it. .
i will try to use your style .
thanks