- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get a list of registered route paths in Laravel?
All the routes are stored inside the routes/ folder. If you open routes/web.php you will see the list of routes defined for your application. If you want to work with routes, you need to include the following class −
use Illuminate\Support\Facades\Route;
The routes/web.php file along with above class is added by default when laravel is installed.
Here is a basic route that gets called when you hit the URL http://localhost:8000/. It is called the default route. The view(‘test’) is called when the URL is hit.
use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('test'); });
Let us understand how to get the list of all routes registered.
Example 1
(Using artisan command)
The command to get all the routes used in the application is −
php artisan route:list
Output
The output of the above code is −
C:\xampp\htdocs\laraveltest>php artisan route:list +--------+----------+---------------------+------+-------------------------------------------------------------+------------------------------------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+---------------------+------+-------------------------------------------------------------+------------------------------------------+ | | GET|HEAD | / | | Closure | web | | | GET|HEAD | api/user | | Closure | api | | | | | | | App\Http\Middleware\Authenticate:sanctum | | | GET|HEAD | sanctum/csrf-cookie | | Laravel\Sanctum\Http\Controllers\CsrfCookieController@show | web | | | GET|HEAD | test | | App\Http\Controllers\StudentController@index | web | | | GET|HEAD | testarray | | App\Http\Controllers\testuserip@index | web | | | GET|HEAD | users | | App\Http\Controllers\UserController@index | web | | | GET|HEAD | validation | | App\Http\Controllers\testvalidationController@showform | web | | | POST | validation | | App\Http\Controllers\testvalidationController@validateform | web | +--------+----------+---------------------+------+-------------------------------------------------------------+------------------------------------------+
Using route:list, the middleware available for each of your route are also displayed. If you want to avoid showing middleware routes you can do as shown in the following example.
Example 2
To avoid middleware in route:list
The command to skip middleware routes in your route list is as follows −
php artisan route:list -v
Output
The output for above is −
C:\xampp\htdocs\laraveltest>php artisan route:list -v +--------+----------+---------------------+------+------------------------------------------------------------+------------------------------------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+---------------------+------+------------------------------------------------------------+------------------------------------------+ | | GET|HEAD | / | | Closure | web | | | GET|HEAD | api/user | | Closure | api | | | | | | | App\Http\Middleware\Authenticate:sanctum | | | GET|HEAD | routes | | Closure | web | | | GET|HEAD | sanctum/csrf-cookie | | Laravel\Sanctum\Http\Controllers\CsrfCookieController@show | web | | | GET|HEAD | test | | App\Http\Controllers\StudentController@index | web | | | GET|HEAD | testarray | | App\Http\Controllers\testuserip@index | web | | | GET|HEAD | users | | App\Http\Controllers\UserController@index | web | | | GET|HEAD | validation | | App\Http\Controllers\testvalidationController@showform | web | | | POST | validation | | App\Http\Controllers\testvalidationController@validateform | web | +--------+----------+---------------------+------+------------------------------------------------------------+------------------------------------------+ C:\xampp\htdocs\laraveltest>
Example 3
Using getRoutes() method
To use the method getRoutes() you need to include the Route class −
use Illuminate\Routing\Router;
Using getRoutes() you will get a RouteCollection. It needs to be looped to get the list of all routes.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Routing\Router; class testuserip extends Controller { public function index(Router $route) { $r = $route->getRoutes(); foreach ($r as $value) { echo $value->uri(); echo "<br/>"; } } }
Output
The output of the above code is as follows −
_ignition/health-check _ignition/execute-solution _ignition/share-report _ignition/scripts/{script} _ignition/styles/{style} sanctum/csrf-cookie api/user / testarray test users validation validation
Example 4
Using getRoutes() by using the Route class in web.php. The getRoutes() will return routeCollection that will hold all the route lists.
use Illuminate\Support\Facades\Route; Route::get('/routes', function() { $routeCollection = Route::getRoutes(); foreach ($routeCollection as $value) { echo $value->getActionName(); echo "<br/>"; } });
Output
The output of the above code is −
Facade\Ignition\Http\Controllers\HealthCheckController Facade\Ignition\Http\Controllers\ExecuteSolutionController Facade\Ignition\Http\Controllers\ShareReportController Facade\Ignition\Http\Controllers\ScriptController Facade\Ignition\Http\Controllers\StyleController Laravel\Sanctum\Http\Controllers\CsrfCookieController@show Closure Closure App\Http\Controllers\testuserip@index App\Http\Controllers\StudentController@index App\Http\Controllers\UserController@index App\Http\Controllers\testvalidationController@showform App\Http\Controllers\testvalidationController@validateform Closure