CodeIgniter Forums

Full Version: Retrieving the default controller config item
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]NemetraL[/eluser]
Hello,

The problem is: I'd need to retrieve
Code:
$route['default_controller'];
from a post-controller-constructor hook.

There's the raw way, which consists in copying the Router library code directly inside the hook:
Code:
// Load the routes.php file.
@include(APPPATH.'config/routes'.EXT);
$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
unset($route);
The point is: this file has already been loaded and the class has already been created (in the system/codeigniter/CodeIgniter.php file, under the name $RTR).

But once the controller is loaded, I can't find it anymore :-S

Any idea?

El Forum

[eluser]NemetraL[/eluser]
Gosh I posted that too fast..

A simple:
Code:
$ci =& get_instance();
$ci->uri->router->routes['default_controller'];
is what I was looking for. I found it using:
Code:
var_dump($ci);
Problem solved.

PS: it's incredibly useful to var_dump() the main controller from time to time to remember all the things it carries with it along the code execution ;-).

El Forum

[eluser]John_Betong[/eluser]
Hi NemetraL,

Have you tried?

Code:
echo "<br /><pre>";
    print_r($ci);
  echo "</pre>";
&nbsp;
or
Code:
echo "<br /><pre>";
    print_r($this);
  echo "</pre>";
&nbsp;
&nbsp;

El Forum

[eluser]NemetraL[/eluser]
Yes the problem is solved now.

I had to go through the var_dump(), which default layout is less readable than the print_r() by the way ;-).

El Forum

[eluser]stormbytes[/eluser]
I wonder if there's a way to do this using CI 2.02 ?

I tried your method and it returns an error.