How to Get All Routes in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hello Friends,

This tutorial shows you how to get all routes in laravel. In this article, we will implement a how to list routes in laravel. We will look at an example of laravel get all routes list. I explained simply step by step laravel routes list.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

I will give you a simple example of how to get an all routes list in laravel application. we will use getRoutes() function of Route facade to get list of all routes in laravel.

So, without further ado please check the below steps:

Step 1: Create Route

In this step, we will add one get-all-routes route to display all routes in laravel. So, let's add a new route to that file.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('get-all-routes', [DemoController::class, 'index']);

Step 2: Create Controller

in the next step, now we have created a new controller as DemoController and write index method on it like as below, So let's create a controller:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Route;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$routes = Route::getRoutes();

return view('routesList', compact('routes'));

}

}

Step 3: Create Blade File

In this step, we need to create one blade file with routesList.blade.php to display all routes, so let's update following code on it:

resources/views/routesList.blade.php

<!DOCTYPE html>

<html>

<head>

<title>How to Get All Routes in Laravel? - ItSolutionStuff.com</title>

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h1>How to Get All Routes in Laravel? - ItSolutionStuff.com</h1>

<table class="table table-bordered data-table">

<thead>

<tr>

<th>Method</th>

<th>URI</th>

<th>Name</th>

<th>Action</th>

</tr>

</thead>

<tbody>

@foreach($routes as $route)

<tr>

<td>{{ $route->methods()[0] }}</td>

<td>{{ $route->uri() }}</td>

<td>{{ $route->getName() }}</td>

<td>{{ $route->getActionName() }}</td>

</tr>

@endforeach

</tbody>

</table>

</div>

</body>

</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/get-all-routes

Output:

I hope it can help you...

Tags :
Shares