Welcome Guest, Not a member yet? Register   Sign In
Loading sub controllers
#1

[eluser]Brandon Dickson[/eluser]
Hello,

I'm relatively new to CI, but I do feel pretty comfortable with it. For the web application I'm developing, I need to load sub controllers into other controllers. Unfortunately I get an error in the loader library when I initialize the new controller.

This is the code for my controller loader, any help would be appreciated.

Code:
<?php
class Controllers
{
    function load( $name )
    {
        $ref_name = end( explode( '/',$name ) );

        $class_name = ucfirst( $ref_name );

        if( isset( $this->$ref_name ) ) return $this->$ref_name;
        
        if( !class_exists( $class_name ) )
        {        
            $path = realpath( APPPATH.'controllers/'.$name.'.php' );
            require( $path );
        }
        
        $this->$ref_name = new $class_name();
        
        return $this->$ref_name;
        
    }
}
?>


Thanks!
-Brandon
#2

[eluser]louis w[/eluser]
Check out Wick.
http://ellislab.com/forums/viewthread/80279/

This was developed to do exactly what you want.
#3

[eluser]Tom Glover[/eluser]
[quote author="louis w" date="1212524068"]Check out Wick.
http://ellislab.com/forums/viewthread/80279/

This was developed to do exactly what you want.[/quote]

I was going to say that! Smile
#4

[eluser]Brandon Dickson[/eluser]
Thanks for the reply, I actually got it sorted, at least for my application,

I extended the base controller class to load an instance of a generic controller, and then property overloading to resolve calls like "$this->load" to the generic controller.


Code:
<?php
class MY_Controller extends Controller
{
    function __construct()
    {
        $this->ci =& get_instance();
        
        if( $this->ci == NULL )
        {
            $this->ci =& new Controller();
        }
    }
    
    public $ci;

    function __get( $var )
    {
        return $this->ci->$var;
    }
}
?>

I don't know enough about the internal workings of CI yet to know if this will work in every situation, but it works for me. I'll probably extend the loader class to access the controllers with $this->load->controller() at some point, if I do I'll post the code for anyone who is interested.

Thanks!
-Brandon
#5

[eluser]louis w[/eluser]
Not sure what you are doing... Why are you you using get_instance? If a controller is loaded inside CI, then you do not need to get the super object, it already exists as $this->
#6

[eluser]Brandon Dickson[/eluser]
Louis,

The fact that MY_Controller extends Controller is really only semantics, I never actually call the parent constructor:
Code:
parent::Controller();
So MY_Controller isn't ever really part of the super object, instead, I create a generic super object with:

Code:
$this->ci =& new Controller();

I call get_instance() to make sure that a super object doesn't already exist.


Any time I access a property in a class that extends MY_Controller, if it can't find the property, it tries to get it from the super object. Thus $this->load is translated into $this->ci->load.

If it doesn't exists in the super object, is just throws the standards error.

Let me know if I've explained this well enough.

Thanks!
-Brandon




Theme © iAndrew 2016 - Forum software by © MyBB