Part of the EllisLab Network
   
 
Loading libraries in subfolder and reflecting the path in $this
Posted: 06 July 2007 05:53 PM   [ Ignore ]  
Summer Student
Total Posts:  25
Joined  06-22-2007

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:

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

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

$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!

<?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;

        
}    

    }     

}

?>

 Signature 

Good luck and happy CI-ing!
.:8):.

Profile
 
 
Posted: 17 July 2007 10:23 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-13-2007

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

 Signature 

Happiness is when what you think, what you say, and what you do are in harmony. - Mahatma Gandhi

Profile
 
 
   
 
 
‹‹ Scaffolding      Helper: Gravatar (Revised) ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 62662 Total Logged-in Users: 34
Total Topics: 77198 Total Anonymous Users: 5
Total Replies: 416743 Total Guests: 239
Total Posts: 493941    
Members ( View Memberlist )