How to Get All Routes in Laravel 10

How to Get All Routes in Laravel 10

In this post we will give you information about How to Get All Routes in Laravel 10. Hear we will give you detail about How to Get All Routes in Laravel 10 And how to use it also give you demo for it if it is necessary.

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 IlluminateSupportFacadesRoute;
  
use AppHttpControllersDemoController;
  
/*
|--------------------------------------------------------------------------
| 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 AppHttpControllers;
  
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
  
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 10 - onlinecode.org</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
        
<div >
    <h1>How to Get All Routes in Laravel 10 - onlinecode.org</h1>
    
    <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:

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 How to Get All Routes in Laravel 10, Hope this code and post will helped you for implement How to Get All Routes in Laravel 10. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US