How to Get All Routes in Laravel 10

Websolutionstuff | Dec-13-2023 | Categories : Laravel

Hey there! This tutorial guides you through the process of retrieving a comprehensive list of all routes in a Laravel 10 application. Throughout this article, we will cover the implementation of a method to list routes in Laravel 10.

The example provided will demonstrate how to obtain a complete list of routes using Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10 versions.

I will illustrate a straightforward example of how to acquire a complete list of routes within a Laravel application. The key to this lies in utilizing the getRoutes() function of the Route facade, which efficiently fetches the list of all routes.

Without further delay, let's dive into the simple step-by-step process to retrieve and display the Laravel routes list.

Step 1: Create a route

Create a route into the web.php file. So, add the below code to that file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\TestController;
  
/*
|--------------------------------------------------------------------------
| 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', [TestController::class, 'getAllRoutes']);

 
Step 2: Create Controller

Use the Artisan command to generate a new controller. Let's name it TestController.

php artisan make:controller TestController

Open the newly created TestController.php file in the app/Http/Controllers directory.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
  
class TestController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getAllRoutes(Request $request)
    {
        $allRoutes = Route::getRoutes();
  
        return view('all-routes', compact('allRoutes'));
    }
}

 

Step 3: Create a Blade View

If you want to display the routes in a web page, create a Blade view. For example, create a file named all-routes.blade.php in the resources/views directory:

<!DOCTYPE html>
<html>
<head>
    <title>How to Get All Routes in Laravel 10 - Websolutionstuff</title>
</head>
<body>
    <h1>All Routes</h1>

    <table border="1">
        <thead>
            <tr>
                <th>Method</th>
                <th>URI</th>
                <th>Name</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($allRoutes as $route)
                <tr>
                    <td>{{ $route->methods()[0] }}</td>
                    <td>{{ $route->uri() }}</td>
                    <td>{{ $route->getName() }}</td>
                    <td>{{ $route->getActionName() }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
</body>
</html>

 

Step 4: Access the Routes

Visit the URL /all-routes in your browser to see a table displaying all routes along with their methods, URIs, names, and actions. And also, you can add middleware.

 

Conclusion:

This method allows you to programmatically retrieve all routes within your Laravel application without using an Artisan command directly.

 


You might also like:

Recommended Post
Featured Post
How To Resize Image Before Upload In Laravel 10
How To Resize Image Before Upl...

In this article, we will see how to resize images before uploading in laravel 10. Here, we will learn about laravel 10&n...

Read More

Mar-29-2023

Laravel 10 Select2 Autocomplete Search Using Ajax
Laravel 10 Select2 Autocomplet...

In this article, we will see laravel 10 select2 autocomplete search using ajax. Here, we will learn about sele...

Read More

Apr-10-2023

How to Convert PDF to Image in Laravel 10
How to Convert PDF to Image in...

Greetings, Laravel enthusiasts! Today, let's unravel a common challenge in web development – converting PDFs t...

Read More

Dec-22-2023

How to Update User Profile in Laravel 11
How to Update User Profile in...

Hello, laravel developers! In this article, we'll see how to update user profiles in laravel 11. Here, we'll cha...

Read More

Jul-12-2024