Codeigniter - Get current route
Asked Answered
A

4

6

I'm looking for help to know which route my Codeigniter application goes through.

In my application folder in config/routes.php i got some database generated routes, could look like this:

$route["user/:any"] = "user/profile/$1";
$route["administration/:any"] = "admin/module/$1";


If i for example to go domain.net/user/MYUSERNAME, then i want to know that i get through the route "user/:any".
Is it possible to know which route it follows?

Adela answered 19/7, 2014 at 20:9 Comment(1)
I think that will be hard to do maybe you want to fetch class or know method that is beeing used? $this->router->fetch_class(); and $this->router->fetch_method();. Or perhaps you can build your routes by function (created by you) with simple $this->uri->segment(n);Devito
A
3

I used @Ochi's answer to come up with this.

$routes = array_reverse($this->router->routes); // All routes as specified in config/routes.php, reserved because Codeigniter matched route from last element in array to first.
foreach ($routes as $key => $val) {
$route = $key; // Current route being checked.

    // Convert wildcards to RegEx
    $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

    // Does the RegEx match?
    if (preg_match('#^'.$key.'$#', $this->uri->uri_string(), $matches)) break;
}

if ( ! $route) $route = $routes['default_route']; // If the route is blank, it can only be mathcing the default route.

echo $route; // We found our route
Adela answered 20/7, 2014 at 11:8 Comment(0)
B
8

One way to know the route could be using this:

$this->uri->segment(1);

This would give you 'user' for this url:

domain.net/user/MYUSERNAME

By this way you can easily identify the route through which you have been through.

Brownell answered 20/7, 2014 at 7:23 Comment(1)
Wanted user/:any. This solution cant help that.Adela
A
3

I used @Ochi's answer to come up with this.

$routes = array_reverse($this->router->routes); // All routes as specified in config/routes.php, reserved because Codeigniter matched route from last element in array to first.
foreach ($routes as $key => $val) {
$route = $key; // Current route being checked.

    // Convert wildcards to RegEx
    $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

    // Does the RegEx match?
    if (preg_match('#^'.$key.'$#', $this->uri->uri_string(), $matches)) break;
}

if ( ! $route) $route = $routes['default_route']; // If the route is blank, it can only be mathcing the default route.

echo $route; // We found our route
Adela answered 20/7, 2014 at 11:8 Comment(0)
B
2

For CodeIgniter 4:

var_dump(service('router')->getMatchedRoute());

It will return something like this:

[
    "{locale}/(.*)",
    "\App\Controllers\Home_controller::any/home"
];

Billbillabong answered 20/1, 2023 at 8:40 Comment(0)
S
1

looking at the latest version it's not possible without using custom router as ROUTEKEY is used and overwritten trying to parse the route

if you would like to create and use custom class it's only a matter of saving original $key to another variable and setting it as a class property for later use when you have a match (line 414 before "return" - you can get that key later as e.g. $this->fetch_current_route_key()) - other thing to remember is that this kind of code modification is easy to break if original class will change (update) so keep that in mind

Schism answered 20/7, 2014 at 8:27 Comment(1)
I made a script which your answer helped me doing. I matched the current URI with the all the routes.Adela

© 2022 - 2024 — McMap. All rights reserved.