In this article, we'll see how to get a list of registered routes in laravel 10. Throughout this article, we will cover the implementation of a method to list routes in Laravel 10. Here, we'll get the full path name in laravel 10.
Laravel provides a command for displaying route names with URI, action, and method. But, we'll create a custom route list table in laravel 10.
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']);
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'));
}
}
If you want to display the routes on 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 List of Registered Routes in Laravel 10 - Vidvatek</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>
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.
You might also like:
- Read Also: Laravel 10 Get Request Header Example
- Read Also: Laravel 10 Setup Cron Job Task Scheduler
- Read Also: How to Create AJAX Pagination in Laravel 10
- Read Also: Laravel 10 Has Many Through Relationship Example