Welcome Guest, Not a member yet? Register   Sign In
controller from controller - once and for all
#1

[eluser]mihailt[/eluser]
time to time someone here asks , how would they call a controller action from another controller, despite the fact that for most cases that is not best solution,
from my point of view, it can be usefull when using in modular approach and grouping your controllers, so to get rid of this problem one can use this function:
Code:
function load_controller($class_name,$init = TRUE){
        if (strpos($class_name, '/') !== FALSE)
        {
            $x = explode('/', $class_name);    
            $class_name = end($x);
            unset($x[count($x)-1]);
            $subdir = implode($x, '/').'/';
        }
        $path = !isset($subdir) ? APPPATH.'controllers/' : APPPATH.'controllers/'.$subdir;
        $fullpath = $path.strtolower($class_name).'.php';
        
        if(file_exists($fullpath)){
            include_once ($fullpath);
            if($init == TRUE){
                $obj = new $class_name;
                return $obj;
            }
            else {
                return TRUE;
            }
        }
        else{
           throw new Exception($class_name.' Class not Found');
        }
    }

the good way of using it would be extending CI_Loader class.
#2

[eluser]xwero[/eluser]
Why not make the function easier for the developers by splitting the class_name variable if a forward slash is found. This is how the other loader functions work.
#3

[eluser]mihailt[/eluser]
[quote author="xwero" date="1236869456"]Why not make the function easier for the developers by splitting the class_name variable if a forward slash is found. This is how the other loader functions work.[/quote]

sure, just modified it.
#4

[eluser]wiredesignz[/eluser]
You cannot simply instantiate a new CI controller and use it for anything substantial.

A new CI Controller also creates a new CI instance, the new instance is not aware of the previous instance or any of its class variables.

Modular Extensions HMVC allows you to call any controller from another. Check it out.
#5

[eluser]mihailt[/eluser]
[quote author="wiredesignz" date="1236872699"]You cannot simply instantiate a new CI controller and use it for anything substantial.

A new CI Controller also creates a new CI instance, the new instance is not aware of the previous instance or any of its class variables.

Modular Extensions HMVC allows you to call any controller from another. Check it out.[/quote]

is that so? from what i've seen any custom controller extends controller class, which extends
CI_Base and calling it's get_instance method which acts as a singleton so no new instance is created, but maybe i'm wrong, i have reviewed HMVC from user perspective and didn't actualy liked it, but, i defenitly will take a look at the source.
#6

[eluser]wiredesignz[/eluser]
get_instance() simply returns the current CI (controller) instance. The singleton pattern is not used there at all.

You may not like HMVC, but it does work. Good luck.
#7

[eluser]mihailt[/eluser]
ok, my bad, didn't paid as much attention to the code, as i should Smile
about HVMC, i didn't said that it didn't, and btw you did a grat job there, well it might be just not my kind of thing.

anyway, just talked with one gay, and he pointed me to this library :
http://zacharias.dynaknudsen.dk/libraries/wick
which is almost the same thing i was tring to achive.
#8

[eluser]xwero[/eluser]
[quote author="wiredesignz" date="1236872699"]A new CI Controller also creates a new CI instance, the new instance is not aware of the previous instance or any of its class variables.
[/quote]
isn't the front controller the file that creates the CI instance?
#9

[eluser]wiredesignz[/eluser]
[quote author="xwero" date="1236877001"]...isn't the front controller the file that creates the CI instance?[/quote]

Yes of course, the bootstrap creates the first controller as $CI.

Any further instances of the Controller class are another instance of $CI also, but, they are not aware of the libraries or models you may have loaded in the first instance. (other than autoload)

CI Models actually suffer this phenomena already, because a newly instantiated Model has none of the CI class variables assigned to it until after its constructor has completed running. Thus you cannot access them inside its constructor even if you load them there.

PHP5 allows fixing these problems very easily with its override methods.
#10

[eluser]xwero[/eluser]
I knew where you where going with this but i just wanted to dot the i Wink

mihailt's function just loads a class like a helper would, it's only a class that extends the CI controller class and that is in the controllers directory.




Theme © iAndrew 2016 - Forum software by © MyBB