title: Codeigniter Call Controller From Controller
body: <p>After the last two comments, I'll dump out my real code and maybe it will help out:</p>
  
  <p>Here is the landing Controller:</p>
  
  <pre><code> &lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Businessbuilder extends CI_Controller {
  
      function __construct()
      {
          parent::__construct();
      }
      function index()
      {
          $RTR = $GLOBALS["RTR"];
  
  
          // import the necessary libraries
          $this-&gt;load-&gt;model("site_pages");
  
          $RTR = $GLOBALS["RTR"];
  
          // get the current site
          $site = current_site();
  
          // get the requesting url
          $class = $RTR-&gt;uri-&gt;rsegments[1];
          $function = $RTR-&gt;uri-&gt;rsegments[2];
  
          // get the current function and class
          $current_method = explode("::", __METHOD__);
  
          // get the real class name that is going to be called
          $site_page = $this-&gt;site_pages-&gt;get(array("display_name"=&gt;$class, "id"=&gt;$site-&gt;id));
          $site_page = $site_page-&gt;result();
          if(count($site_page) == 1)
          {
              $site_page = $site_page[0];
  
              // set the class name to be called
              $class = $site_page-&gt;class_name;
          }
  
          // only execute if the requested url is not the current url
          if(!(strtolower($class) == strtolower($current_method[0]) &amp;&amp; strtolower($function) == strtolower($current_method[1])))
          {
              if(!file_exists(APPPATH.'controllers/'.$RTR-&gt;fetch_directory().$class.EXT))
              {
                  show_404($RTR-&gt;fetch_directory().$class);
                  exit;
              }
  
              // include the required file. I use require once incase it is a file that I've already included
              require_once(APPPATH.'controllers/'.$RTR-&gt;fetch_directory().$class.EXT);
  
              // create an instance of the class
              $CI = new $class();
  
              if(method_exists($CI, $function))
                  // call the method
                  call_user_func_array(array(&amp;$CI, $function), array_slice($RTR-&gt;uri-&gt;rsegments, 2));
              else
              {
                  show_404($RTR-&gt;fetch_directory().$class);
                  exit;
              }
          }
      }
  }
  </code></pre>
  
  <p>here is an example of a dynamic controller that will be called:</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Public_homepage extends CI_Controller {
  
      function __construct()
      {
          parent::__construct();
      }
  
      function index()
      {
          echo "&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;";
          $this-&gt;load-&gt;model("sites");
  
          $style = $this-&gt;sites-&gt;get(array("id"=&gt;1)); // fail here, sites not defined
          //print_r($style);
          exit;
  
  
          $view_params = array();
          $view_params["site_id"] = $this-&gt;site_id;
  
          $this-&gt;load-&gt;view('public_homepage', $view_params);
      }
  }
  </code></pre>
  
  <p>Here is my model that I am using:</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Sites extends CI_Model
  {
      function __construct()
      {
          parent::__construct();
      }
  
      function get($search = array())
      {
          return $this-&gt;db-&gt;query("SELECT * FROM sites"); // failure on this line, db undefined
          }
  }
  </code></pre>
  
  <p>the error that I am getting is either this (error1):</p>
  
  <pre><code>A PHP Error was encountered
  
  Severity: Notice
  
  Message: Undefined property: Public_homepage::$sites
  
  Filename: controllers/public_homepage.php
  
  Line Number: 15
  Fatal error: Call to a member function get() on a non-object in /var/www/businessbuilderapp.com/public_html/application/controllers/public_homepage.php on line 15
  </code></pre>
  
  <p>or this (error2):</p>
  
  <pre><code>A PHP Error was encountered
  
  Severity: Notice
  
  Message: Undefined property: Businessbuilder::$db
  
  Filename: core/Model.php
  
  Line Number: 50
  Fatal error: Call to a member function query() on a non-object in /var/www/businessbuilderapp.com/public_html/application/models/bba_model.php on line 25 
  </code></pre>
  
  <p>My theory as to why I am getting these errors is because the instance of the object is different than the one that loaded the model and libraries. What's odd about that though is that arrays are carried over, but not objects. So in the core Loader.php of codeigniter array $_ci_models is populated with models that are not loaded in the Public_homepage class</p>
  
  <p>Also what might help you is that from the first pass through the businessbuilder class, I am able to load and use the modules successfully, but when Public_homepage is called, that's when things start to fail.</p>
  
  <p>What makes this confusing is that I'm trying to figure out 2 errors with one question which is probably my mistake. Here is a description of when I get the errors:</p>
  
  <p>Error1:</p>
  
  <p>When I run the code as is, I cannot call the sites property.</p>
  
  <p>Error2:</p>
  
  <p>When I change the 
  call_user_func_array(array(&amp;$CI, $function), array_slice($RTR->uri->rsegments, 2));
  to
  eval($class . "->" . $function);</p>
  
  <p>I understand that this is really confusing, especially when I explain it, but if you need more info, please let me know. Also note that the Public_homepage looks like that because I am testing. There's no need to dump more useless lines if the error can be produced with minimal code. </p>
  
  <h2>Update</h2>
  
  <p>After reading some of the answers, I realized that I didn't explain the code. What this code does is that it allows me to store different urls inside a database, but all the urls stored there can call the same page even though they are different. I guess an exact example would be changing the slug on wordpress.</p>
  
  <p>What happens is that the businessbuilder class is set to accept ALL requests to the server. When it hits the businessbuilder class, it will access the database, find out what sub url you are using, find the real controller that the user is looking for, and access that controller. </p>