Welcome Guest, Not a member yet? Register   Sign In
How do I call a controller within a controller
#1

(This post was last modified: 09-09-2015, 06:41 PM by dondo.)

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

Reply
#2

Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(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
Reply
#4

(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. 
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB