CodeIgniter Forums

Full Version: How do I call a controller within a controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This code works before in CI2:

Inside /core/MY_loader.php
------------------------------------

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

class 
MY_Loader extends CI_Loader{

 
   function __construct(){
 
       parent::__construct();
 
   }

 
   function controller($file_name,$directory=""){
 
       $CI = & get_instance();
 
       
        
// if the controller exists inside a directory
 
       $directory = ($directory != "") ? $directory."/" "";

 
       $file_path APPPATH.'controllers/'.$directory.ucfirst($file_name).'.php';
 
       $object_name $file_name;
 
       $class_name ucfirst($file_name);
 
     
        if
(file_exists($file_path)){
 
           require $file_path;
 
           if(class_exists($class_name)) {
 
               $CI->$object_name = new $class_name();
 
               
            
}
 
           else {
 
               show_404();
 
           }
 
       }
 
       else{
 
           //show_error("Unable to load the requested controller class: ".$class_name);
 
        show_404();
 
       }
 
   }
}

?>


Inside /application/controllers/controller.php (this is my controller that wants to call another controller)
------------------------------------------------------------------------
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class 
Controller extends CI_Controller {
 public function 
__construct(){
        parent::__construct();

    }

 function 
index() {
 
// This will call $this->sample;
 
$this->load->controller('sample','products');
 }
}
?>


Inside /application/controllers/products/sample.php (sample controller that supposedly been called by controller.php)

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

class 
Sample extends CI_Controller {
 public function 
__construct(){
 
       parent::__construct();

 
   }

 function 
index() {
 echo 
"test";
 }

Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do?
(09-12-2015, 10:12 AM)includebeer Wrote: [ -> ]Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do?

Because I have a dynamic URL. 

Sample: 
http://test.com/<username> -> this will go to user profile controller
or
http://test.com/<storename> -> this will go to store profile controller
(09-13-2015, 09:03 PM)dondo Wrote: [ -> ]
(09-12-2015, 10:12 AM)includebeer Wrote: [ -> ]Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do?

Because I have a dynamic URL. 

Sample: 
http://test.com/<username> -> this will go to user profile controller
or
http://test.com/<storename> -> this will go to store profile controller

I would figure out a different way of doing it then. Calling a controller from inside a controller completely defeats the purpose of MVC.

I see what you are trying to do. Some sites get around this by making the url have a trailing letter for example "/u/<username>" and /s/<storename>". 

If you need to use the same code in 2 controllers, that piece of code should be put in a library or a model. 
In this case, I would recommend either setting up a method to determine the routes dynamically or setup one or more _remap() methods to handle the requests.