Share this:

Hello Dev, Today we are going to learn on How to Get All Routes in Laravel? This tutorial will cover on getting all the routes in laravel 9 application.

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 and laravel 9 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:

Steps on How to Get All Routes in Laravel?

  • Step 1: Create Route
  • Step 2: Create Controller
  • Step 3: Create Blade File
  • Step 4: Testing
  • Step 5: Conclusion

Also Read: How to Truncate String in Laravel?

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

Also Read: How to Convert Image to Base64 in Laravel?

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

Also Read: How to Get Browser Name and Version in Laravel 9?

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? - LaravelTuts.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? - LaravelTuts.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>

Also Read: Laravel 9 Custom Login and Registration Example

Step 4: Testing

So, Everything is done. Now we are going to test our application. 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://127.0.0.1:8000/get-all-routes

Output:

Laravel Get All Routes
Laravel Get All Routes

Step 5: Conclusion

Today, We had learn How to Get All Routes in Laravel? Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to Integrating AdminLTE 3 in Laravel 9?

Question Asked

  • How do I see all of the routes that are defined in Laravel?
  • How can I get route in Laravel?
  • What is the command to list all routes?
  • What is Route any in Laravel?
Share this:

Categorized in: