How to get a list of registered route paths in Laravel?

126,900

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

Output demo

Share:
126,900
Kevin Jung
Author by

Kevin Jung

Updated on November 10, 2021

Comments

  • Kevin Jung
    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
    Rajan Rawal over 10 years
    how did you iterate through collection?
  • parrker9
    parrker9 almost 9 years
    Better change the last line of the Closure to return '<pre>' . \Artisan::output() . '</pre>'; for better formatting.
  • Mohammed
    Mohammed over 8 years
    This is php artisan route:list now
  • poashoas
    poashoas over 8 years
    Can I use this in a custom helper?
  • Andrew Brown
    Andrew Brown about 8 years
    @RajanRawal Collections implement ArrayAccess which allows them to behave just like arrays, and be used in the foreach.
  • Andrew Brown
    Andrew Brown almost 8 years
    if you prefer to avoid Facades, you can inject Illuminate\Routing\Router.
  • Zachary Weixelbaum
    Zachary Weixelbaum about 7 years
    That is true for Laravel 5, but the question was for Laravel 4
  • Fabio says Reinstate Monica
    Fabio says Reinstate Monica about 7 years
    Can you please edit your answer and add a short explanation about what it does and how it works? Thank you!
  • utdev
    utdev about 7 years
    Ány Idea how to filter this? For example for routes which start like this api/
  • ErvTheDev
    ErvTheDev almost 7 years
    Laravel 5.2.* $routeCollection = Route::getRoutes()->getRoutes(); dd($routeCollection);
  • Robert Johnstone
    Robert Johnstone almost 7 years
    This is not true of 5.4
  • Gideon Maina
    Gideon Maina over 6 years
    @FabioTurati We are just getting all the routes from Laravel getRoutes() method, then sending them to the template, then finally create a normal html table with the data while looping through all of them. For instance we are displaying the uri e.g /home, name e.g home_route you assigned and the others.
  • siannone
    siannone over 6 years
    Just 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
    Vicky Gill over 5 years
    Works for Laravel 5.6.x Thanks
  • Meow Kim
    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
    Yevgeniy Afanasyev over 3 years
    use Illuminate\Support\Facades\Route
  • feeela
    feeela about 2 years
    php artisan route:list returns “There are no commands defined in the "route" namespace.” in Laravel 8. Any suggestions?