How to Get All Routes in Laravel 10

Spread the love

Hello Guys,

Learn How to Get All Routes in Laravel 10 – A Step-by-Step Guide

In this tutorial, we’ll guide you through the process of obtaining a comprehensive list of routes in Laravel 10. We’ll explore how to effectively use Laravel’s route listing functionality. You’ll find a practical example of listing all routes in Laravel.

Discover two simple methods to retrieve an all-routes list within your Laravel application. We’ll show you how to make use of the getRoutes() function provided by the Route facade and leverage the artisan route:list command to obtain an exhaustive Laravel routes list.

Get All Routes in Laravel 10 Examples:

Use the getRoutes() :

To showcase all Laravel routes, we’ll establish the “get-all-routes” route. Let’s now delve into the following example for a comprehensive understanding.

Also Read: How to use Multiple Database Connections in LARAVEL 10

<?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']);

Create a Controller:

To create an index method for a fresh controller, it’s essential to name it “DemoController.” Explore the accompanying example:

<?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('route-list', compact('routes'));
    }
}

Create a Blade File:

To showcase all available routes, it’s imperative to create a unified blade file named “routesList.blade.php.” Now, let’s delve into an illustrative example:

Also Read: How to use One to Many Eloquent Relationship in LARAVEL 10

<!DOCTYPE html>
<html>
<head>
    <title>How to Get All Routes in Laravel 10 - itcodestuff.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 10 - itcodestuff.com</h1>
    
    <table class="table table-bordered">
        <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 your Laravel app:

To get your Laravel application up and running, follow these steps: Execute the provided command below and hit Enter after taking all the necessary actions.

php artisan serve

Now, Enter the provided URL into your web browser to view the app’s output.

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

Use artisan route:list command

To discover a comprehensive index of your application’s registered routes, simply employ the “route:list” command. This command reveals a wealth of information, including the domain, HTTP method, URI, route name, associated action, and middleware for every route elegantly presented in the generated table.

For a clear illustration, the command is showcased without any additional options in the following example.

Also Read: How to INTEGRATION Razorpay Payment Gateway in Laravel 10

php artisan route:list

Dive into our collection of Laravel blogs – your go-to source for expert insights.

Leave a Comment