How to Get All Routes in Laravel 9 - TechvBlogs

How to Get All Routes in Laravel 9

In this article, you will learn How to Get All Routes using Laravel 9.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

1 year ago

TechvBlogs - Google News

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

I will give a two simple examples of getting an all-routes list in the laravel application. We will use the getRoutes() function of the Route facade and artisan route:list command to get a list of all routes in laravel.

Example 1: Use getRoutes() function

Step 1: Create Route

We will add a get-all-routes route to display all routes in Laravel. So, let's look at the following example.

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

We have to create a new controller as DemoController and write an index method on it like the one below. So, let's look at the following example:

app/Http/Controllers/DemoController

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

Step 3: Create Blade File

We need to create one blade file with routesList.blade.php to display all routes, So, let's look at the following example:

resources/views/route-list.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to Get All Routes in Laravel 9 - techvblogs.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 9 - techvblogs.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 Laravel:

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

Example 2: Use artisan route:list command

The route:list command can be used to show a list of all the registered routes for the application. This command will display the domain, method, URI, name, action and middleware for the routes it includes in the generated table.

The following example demonstrates how to use the command without any options:

php artisan route:list

Thank you for reading this article.

Read Also: How to Increment Multiple Column Values in Laravel 9

Comments (0)

Comment


Note: All Input Fields are required.