Welcome Guest, Not a member yet? Register   Sign In
CI Router Class...?
#1

[eluser]ZaLiTHkA[/eluser]
I stumbled across this post just now, which got me interested in learning more about how to use this 'router' class [source]:
[quote author="InsiteFX" date="1326637261"]You people should really take a look at the CI Router Class!

Code:
// fetch the Controller!
$controller = $this->CI->router->fetch_class();

// fetch the method!
$method = $this->CI->router->fetch_method();

// fetch the directory!
$directory = $this->CI->router->fetch_directory();

// fetch all controller methods
// --------------------------------------------------------------------

/**
  * get_controller_methods()
  *
  * Description:
  *
  * @access public
         * @param       string
  * @return array
  */
public function get_controller_methods($controller_name)
{
  $ctrl_methods = array();

  $ctrl_methods = get_class_methods($controller_name);

  return $ctrl_methods;
}
[/quote]

After a little bit of digging, I found it to be one of the core classes. I'd like to learn more about this, how it works, why it works that way, etc etc.. Unfortunately I didn't see anything too helpful in my forum search, and the only area of the CI user docs that I can find relating to core classes is how to override or extend them with a simple list of the core classes [link].

Did I maybe miss something in the documentation? Or were these classes designed with the idea of 'internal CI' only stuff?
#2

[eluser]PhilTem[/eluser]
There is basically not a lot of documentation or information on the core classes. The only thing you can do, is to open index.php and go with the flow of data to understand everything about CI - and especially how the router class works.
That's what I did when wanting to know what CI is actually doing on every request. Really helps you understand stuff better Wink
#3

[eluser]ZaLiTHkA[/eluser]
So apparently using...
Code:
$this->CI->router->fetch_class();
...in my controller doesn't work. All three (fetch_class(), fetch_method() and fetch_directory()) work within the scope of the current controller if I remove the 'CI->' and run it as...
Code:
$this->router->fetch_class();

Next logical step was to dig through the "/system/core/Router.php" file. Unfortunately it was getting me quite lost and confused, so in my current test app I've done the following to try see what I have to work with:

Code:
/* In Controller: */
$this->data['router_object_test'] = $this->router;
$this->load->view('view_loader', $this->data);

/* In View */
var_dump($router_object_test);

Here's the var_dump output (I removed the config values as they're irrelevant here)
Code:
object(CI_Router)[7]
  public 'config' => &
    object(CI_Config)[3]
      public 'config' => &
        array
          /*irrelevant code removed*/
      public 'is_loaded' =>
        array
          0 => string 'application/config/ion_auth.php' (length=31)
      public '_config_paths' =>
        array
          0 => string 'application/' (length=12)
  public 'routes' =>
    array
      'default_controller' => string 'main' (length=4)
  public 'error_routes' =>
    array
      empty
  public 'class' => string 'main' (length=4)
  public 'method' => string 'index' (length=5)
  public 'directory' => string '' (length=0)
  public 'default_controller' => string 'main' (length=4)
  public 'uri' => &
    object(CI_URI)[6]
      public 'keyval' =>
        array
          empty
      public 'uri_string' => string '' (length=0)
      public 'segments' =>
        array
          empty
      public 'rsegments' =>
        array
          1 => string 'main' (length=4)
          2 => string 'index' (length=5)
      public 'config' => &
        object(CI_Config)[3]
          public 'config' => &
            array
              ...
          public 'is_loaded' =>
            array
              ...
          public '_config_paths' =>
            array
              ...

I've managed to work out how to list a controller's methods (not shown here), but I still don't see anything here that could be used to list the actual controllers... Could somebody please push me off the right side of the cliff here? I'm really not quite sure where to go.. :/
#4

[eluser]PhilTem[/eluser]
You should be able to get the current controller via

Code:
$this->router->fetch_class();

in your controller (won't work in a library, but will also work in models). For libraries and helpers you need to do something like

Code:
get_instance()->router->fetch_class();

Since $this in scope of controllers refers to the CI super-object (i.e. to get_instance()) you cannot use $this->CI in your controllers as it is not even set Wink
#5

[eluser]ZaLiTHkA[/eluser]
[quote author="PhilTem" date="1349267714"]Since $this in scope of controllers refers to the CI super-object (i.e. to get_instance()) you cannot use $this->CI in your controllers as it is not even set Wink[/quote]

Suddenly this makes a lot more sense.. I guess there are obvious disadvantages to doing all this without any proper training in PHP. Smile Everything I know about it is from simply asking questions, searching Google, or trial and error.

Anyways, I'll play around with this tonight and see what I can do. Thanks for the advice. Smile




Theme © iAndrew 2016 - Forum software by © MyBB