Welcome Guest, Not a member yet? Register   Sign In
help getting all routes in an array in controller
#1

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

namespace App\Controllers;
use 
Config\Services;
use 
Config\Routes;
class 
Test extends BaseController
{
    public function index() {
        try {    
            $routes 
Services::routes(true);
            $this->response->setJSON(print_r($routestrue));
            $this->response->send();
        
        catch (\Exception | \Throwable $e) {
            var_dump($e);
            exit;
        }
    }

But I'm having problems getting the actual array out of that.  I can't see how to do something like $routes->routes since it is protected.  If I try something like:
PHP Code:
$services = new Services();
print_r($services->routes(true); 

It's an undefined method.  There's a piece of OOP syntax there that I'm missing.
Any ideas?
Thanks!
Reply
#2

A quick followup that partially works.  I can do:
PHP Code:
include(APPPATH 'Config\Routes.php');
var_dump($routes->getRoutes());
exit; 

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.  
Reply
#3

$r = service('routes');
$r->setHTTPVerb('post'); // verb are lowercase
$r->getRoutes());
Reply
#4

Why do you want to get all routes?
Reply
#5

(This post was last modified: 10-15-2023, 06:28 PM by kenjis.)

(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();
$routes $router->getRoutes();
dd($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();
        $routes $router->getRoutes("post");
        dd($routes); 
and to get the get routes:

PHP Code:
$router Services::routes();
        $routes $router->getRoutes("get");
        dd($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("*");
//see codeigniter4/framework/system/Router/RouteCollection.php line 193 

will not return any thing
Reply
#6

See https://codeigniter4.github.io/CodeIgnit...ing-routes
Reply




Theme © iAndrew 2016 - Forum software by © MyBB