5

I've tried the following but the return is null.

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider {
    public function boot() {

        $routeList = Route::getRoutes();

        foreach ($routeList as $value) {
            dd($value->getPath());
        }

    }

}

My route file:

<?php    
Route::namespace('admin')->group(function () {
    Route::get('admin/post', 'PostController@index')->name('posts');
    Route::get('admin/post/new', 'PostController@new')->name('post_new');
    Route::post('admin/post/save', 'SubjectController@save')->name('post_save');
});

I tried several ways, but I can not list the routes created in the web.php routes file

2 Answers2

7

If you want to use them in your controller to use the programmatically, then you can access them through the Route Class via Route::getRoutes().

    use \Route;
    dd(Route::getRoutes());

To review/analyze the list of routes however, I just use the artisan command line in the root of your application:

php artisan route:list

If you have a bash, you can even look for specific routes with grep.

php artisan route:list |grep users

Hope this helps.

Dom DaFonte
  • 1,619
  • 14
  • 31
5

This will provide every details about routes.

$routes = app('router')->getRoutes();

return  $arrays=(array) $routes;
Sohel0415
  • 9,523
  • 21
  • 30
  • `getRoutes()` method gives out the `RouteCollection` object, which contains 4 variables with duplicated data in them. In order to specifically get All routes, I did: `app('router')->getRoutes()->get();` – Madhur Bhaiya Feb 24 '20 at 10:11