CodeIgniter Forums
How do I call a controller within a controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How do I call a controller within a controller (/showthread.php?tid=62933)



How do I call a controller within a controller - dondo - 09-09-2015

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";
 }




RE: How do I call a controller within a controller - includebeer - 09-12-2015

Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do?


RE: How do I call a controller within a controller - dondo - 09-13-2015

(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


RE: How do I call a controller within a controller - albertleao - 09-14-2015

(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. 


RE: How do I call a controller within a controller - mwhitney - 09-14-2015

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.