How to Retrieve the Full Laravel Route List: Complete Guide to Inspecting Application Endpoints
Use the php artisan route:list command to display every registered route, or access the router directly via Route::getRoutes() for programmatic inspection.
When building or auditing a Laravel application, you often need to verify all available endpoints and their corresponding controllers or closures. The laravel/framework repository provides robust tooling to retrieve the full Laravel route list through both command-line and programmatic interfaces.
Understanding the Route List Command Architecture
The route:list Artisan command is implemented in src/Illuminate/Foundation/Console/RouteListCommand.php. This command is automatically registered within the framework’s service container by src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php, ensuring it is available in every Laravel application.
When executed, the command performs the following operations:
- Collects routes from the router instance using
$this->router->getRoutes(). - Transforms each route into a structured array containing the domain, HTTP methods, URI, name, action (controller or closure), and middleware via
getRouteInformation(). - Applies filtering based on command-line options (
--method,--name,--action,--path) infilterRoute(). - Sorts the collection (default by URI) using
sortRoutes(). - Formats output for console display (
forCli()) or JSON export (asJson()).
How to Retrieve the Full Laravel Route List via CLI
Displaying All Routes in a Table
To view every registered route in a formatted console table, run the following command from your project root:
php artisan route:list
This outputs a table with columns for Domain, Method, URI, Name, Action, and Middleware, allowing you to quickly scan your application’s endpoint structure.
Exporting Routes to JSON
For programmatic consumption or documentation generation, export the route list as JSON:
php artisan route:list --json > routes.json
The --json flag triggers the asJson() method in RouteListCommand.php, returning a machine-readable array of route objects.
Filtering the Route Collection
When working with large applications, filter the output to locate specific endpoints:
# Show only GET routes
php artisan route:list --method=GET
# Find routes by name pattern
php artisan route:list --name=admin.*
# Filter by controller action
php artisan route:list --action=UserController@index
# Search by URI path
php artisan route:list --path=api/v1
These options map to the filterRoute() method, which validates each route against your criteria before display.
Programmatically Accessing Route Information
Using the Artisan Facade
To retrieve the full Laravel route list from within your application code, use the Artisan facade:
use Illuminate\Support\Facades\Artisan;
// Execute the route:list command with JSON output
Artisan::call('route:list', ['--json' => true]);
$routes = json_decode(Artisan::output(), true);
// Iterate through routes
foreach ($routes as $route) {
echo sprintf(
"%s %s => %s\n",
$route['method'],
$route['uri'],
$route['action']
);
}
This approach leverages the same RouteListCommand.php logic while allowing you to process the results within your PHP code.
Direct Router Access with the Route Facade
For custom processing without invoking Artisan, access the router directly:
use Illuminate\Support\Facades\Route;
// Retrieve the raw RouteCollection
$routes = Route::getRoutes();
// Transform to array with custom formatting
$routeInfo = collect($routes)->map(function ($route) {
return [
'method' => implode('|', $route->methods()),
'uri' => $route->uri(),
'name' => $route->getName(),
'action' => $route->getActionName(),
'middleware' => $route->gatherMiddleware(),
'domain' => $route->getDomain(),
];
});
// Output for inspection
dd($routeInfo->toArray());
This method provides access to the underlying Illuminate\Routing\Route objects, exposing methods like gatherMiddleware() and getDomain() that may not appear in the standard CLI output.
Summary
- The
route:listArtisan command, implemented inRouteListCommand.php, is the primary tool to retrieve the full Laravel route list. - Use
--jsonto export routes for programmatic consumption, or--method,--name, and--pathfilters to narrow results. - Access routes programmatically via
Artisan::call('route:list')or directly throughRoute::getRoutes()for custom inspection logic. - The command sources data from the
Routerclass insrc/Illuminate/Routing/Router.php, ensuring consistency between CLI and programmatic access.
Frequently Asked Questions
Where is the route:list command implemented in Laravel?
The command is implemented in src/Illuminate/Foundation/Console/RouteListCommand.php within the laravel/framework repository. It is registered as a service in src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php, making it available automatically in all Laravel applications.
Can I filter routes by middleware using route:list?
The standard route:list command does not provide a dedicated --middleware filter option. However, middleware information is displayed in the output table under the Middleware column. To filter by middleware programmatically, use Route::getRoutes() and inspect the gatherMiddleware() method on each route object.
How do I export my Laravel route list to JSON programmatically?
Use the Artisan facade to execute the command with the --json flag and capture the output:
Artisan::call('route:list', ['--json' => true]);
$routes = json_decode(Artisan::output(), true);
This returns an array of route objects containing URI, method, action, and middleware details suitable for API documentation or automated testing tools.
What is the difference between using Artisan::call and Route::getRoutes()?
Artisan::call('route:list') invokes the full command pipeline, applying filters, sorting, and formatting defined in RouteListCommand.php, returning human-readable or JSON output. Route::getRoutes() provides direct access to the raw RouteCollection objects, allowing you to bypass command logic and implement custom filtering, formatting, or analysis without the overhead of the Artisan console.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →