Dhruv Sompura's avatar

How to get all routes list that i added?

I want to show all the routes list that i created in web.php file. Because i want to give permissions to user based on this routes. How can i get this route lists ? Please help me on this. I am using this code , but this code is not giving me my registered routes. use Illuminate\Support\Facades\Route; $routeCollection = Route::getRoutes();

0 likes
10 replies
Dhruv Sompura's avatar

Thank you for reply. My controller is in admin folder and i am getting this error Class 'App\Http\Controllers\admin\Illuminate\Support\Facades\Route' not found

Nakov's avatar

@Dhruv Sompura because you are using wrong import..

above your class definition where you have use ... add this:

use Illuminate\Support\Facades\Route;

class admin... 

public function index()
{
 $routes =	Route::getRoutes();
}
1 like
Dhruv Sompura's avatar

Thank you @Nakov. Get path error Method Illuminate\Routing\Route::getPath does not exis

Nakov's avatar

@Dhruv Sompura if you do this:

 $routes =	Route::getRoutes();

foreach ($routes as $route) {
	dd($route);
}

what do you get?

1 like
Nakov's avatar
Nakov
Best Answer
Level 73

@Dhruv Sompura try this:

$routes = collect(Route::getRoutes())
	->map(function ($route) { 
			return $route->uri(); 
	});
dd($routes);
1 like
Dhruv Sompura's avatar

Superb. thank you so much. I have one more query in my mind. I am creating different folders for routes, so can i get routes from specific folders?

Nakov's avatar

@Dhruv Sompura Check the RouteServiceProvider and you'll see how the routes are being initialized. So do the same and you should be able to list them all.

1 like

Please or to participate in this conversation.