help getting all routes in an array in controller |
Hi,
In my controller, I'm trying to get all of the currently defined routes into an array. It doesn't matter how the array is set up really, whether its $array['GET'] or $array[URI_PATH], I can take it from there. I can get very close with PHP Code: <?php PHP Code: $services = new Services(); It's an undefined method. There's a piece of OOP syntax there that I'm missing. Any ideas? Thanks!
A quick followup that partially works. I can do:
PHP Code: include(APPPATH . 'Config\Routes.php'); Which is a really bad idea, but $routes->getRoutes() will give me all of the available GET routes. The docs say that that this Returns the raw array of available routes. (https://codeigniter4.github.io/api/class..._getRoutes) which is partially true. It seems to only return the routes available to the current http request type. So if I do GET myController.php it shows me only the GET routes. If I do POST myController.php it only shows me the POST routes. $routes->setHTTPVerb('POST'); sounds like it ought to work, but it doesn't. If I do a GET request and set the verb to POST, I only get an empty array.
$r = service('routes');
$r->setHTTPVerb('post'); // verb are lowercase $r->getRoutes());
Why do you want to get all routes?
(12-12-2022, 07:26 PM)kenjis Wrote: Why do you want to get all routes? In Routes.php I defined url identifier , like this PHP Code: $routes->get('(:any)', 'Go::go/$1', ['filter' => 'webratelimit']); When creating a new URL identifier, the identifier should not already exist as a valid CodeIgniter route. This is because if a URL identifier exists as a valid route, then it will conflict with the route, and the route will not be able to be accessed. SO I need which rooutes are defined I use this code to get all routes as array: PHP Code: $router = Services::routes(); but the returned array not contains all of defined routes is just get a scope of them and not all of them. I solved it speicify which http verb I want to get for example: PHP Code: $router = Services::routes(); PHP Code: $router = Services::routes(); because some times may be you are on post method and if you do $router->getRoutes you will get only post routes. because I notice that using PHP Code: $router->getRoutes("*"); will not return any thing |
Welcome Guest, Not a member yet? Register Sign In |