How to get a list of registered route paths in Laravel?
Solution 1
Route::getRoutes()
returns a RouteCollection
. On each element, you can do a simple $route->getPath()
to get path of the current route.
Each protected parameter can be get with a standard getter.
Looping works like this:
$routeCollection = Illuminate\Support\Facades\Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
Solution 2
You can use console command:
Laravel 4 as asked in question
php artisan routes
Laravel 5 more actual
php artisan route:list
Helpers (Laravel 4) :
Usage:
routes [--name[="..."]] [--path[="..."]]
Options:
--name Filter the routes by name.
--path Filter the routes by path.
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction (-n) Do not ask any interactive question.
--env The environment the command should run under.
Solution 3
For Laravel 5, you can use artisan command
php artisan route:list
instead of php artisan routes
.
Solution 4
I created a route that will list each route and its respective details in an html table.
Route::get('routes', function() {
$routeCollection = Route::getRoutes();
echo "<table style='width:100%'>";
echo "<tr>";
echo "<td width='10%'><h4>HTTP Method</h4></td>";
echo "<td width='10%'><h4>Route</h4></td>";
echo "<td width='10%'><h4>Name</h4></td>";
echo "<td width='70%'><h4>Corresponding Action</h4></td>";
echo "</tr>";
foreach ($routeCollection as $value) {
echo "<tr>";
echo "<td>" . $value->getMethods()[0] . "</td>";
echo "<td>" . $value->getPath() . "</td>";
echo "<td>" . $value->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
Solution 5
Improving @jeanfrg's answer
It has a few deprecated functions. It shows error while editing an answer, hence posting it here.
Laravel 6, 7 & 8
Put it inside routes/web.php
Route::get('routes', function () {
$routeCollection = Route::getRoutes();
echo "<table style='width:100%'>";
echo "<tr>";
echo "<td width='10%'><h4>HTTP Method</h4></td>";
echo "<td width='10%'><h4>Route</h4></td>";
echo "<td width='10%'><h4>Name</h4></td>";
echo "<td width='70%'><h4>Corresponding Action</h4></td>";
echo "</tr>";
foreach ($routeCollection as $value) {
echo "<tr>";
echo "<td>" . $value->methods()[0] . "</td>";
echo "<td>" . $value->uri() . "</td>";
echo "<td>" . $value->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
Demo:
Access it via <url>/routes
Kevin Jung
Updated on November 10, 2021Comments
-
Kevin Jung over 2 years
I was hoping to find a way to create an array with the registered routes paths within Laravel 4.
Essentially, I am looking to get a list something like this returned:
/ /login /join /password
I did come across a method
Route::getRoutes()
which returns an object with the routes information as well as the resources but the path information is protected and I don't have direct access to the information.Is there any other way to achieve this? Perhaps a different method?
-
Rajan Rawal over 10 yearshow did you iterate through collection?
-
parrker9 almost 9 yearsBetter change the last line of the Closure to
return '<pre>' . \Artisan::output() . '</pre>';
for better formatting. -
Mohammed over 8 yearsThis is
php artisan route:list
now -
poashoas over 8 yearsCan I use this in a custom helper?
-
Andrew Brown about 8 years@RajanRawal
Collection
s implementArrayAccess
which allows them to behave just like arrays, and be used in theforeach
. -
Andrew Brown almost 8 yearsif you prefer to avoid Facades, you can inject
Illuminate\Routing\Router
. -
Zachary Weixelbaum about 7 yearsThat is true for Laravel 5, but the question was for Laravel 4
-
Fabio says Reinstate Monica about 7 yearsCan you please edit your answer and add a short explanation about what it does and how it works? Thank you!
-
utdev about 7 yearsÁny Idea how to filter this? For example for routes which start like this
api/
-
ErvTheDev almost 7 yearsLaravel 5.2.*
$routeCollection = Route::getRoutes()->getRoutes(); dd($routeCollection);
-
Robert Johnstone almost 7 yearsThis is not true of 5.4
-
Gideon Maina over 6 years@FabioTurati We are just getting all the routes from Laravel
getRoutes()
method, then sending them to thetemplate
, then finally create a normalhtml table
with the data while looping through all of them. For instance we are displaying theuri e.g /home
,name e.g home_route
you assigned and the others. -
siannone over 6 yearsJust in case someone finds it useful, with Laravel >= 5.5 you can use:
$routes = array_map(function (\Illuminate\Routing\Route $route) { return $route->uri; }, (array) Route::getRoutes()->getIterator());
-
Vicky Gill over 5 yearsWorks for Laravel 5.6.x Thanks
-
Meow Kim over 3 years@utdev I know this is old comments, you can pass the parameter to filter like this
\Artisan::call('route:list', ['--path' => 'api']);
-
Yevgeniy Afanasyev over 3 yearsuse Illuminate\Support\Facades\Route
-
feeela about 2 years
php artisan route:list
returns “There are no commands defined in the "route" namespace.” in Laravel 8. Any suggestions?