109

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Kevin Jung
  • 2,973
  • 7
  • 32
  • 35
  • 1
    possible duplicate of [Displaying registered routes in Laravel 4](http://stackoverflow.com/questions/15955295/displaying-registered-routes-in-laravel-4) – mniess Jun 08 '14 at 16:27

13 Answers13

143

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();
}
Lundis
  • 371
  • 1
  • 2
  • 13
netvision73
  • 4,831
  • 1
  • 24
  • 30
99

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.
NoOorZ24
  • 2,914
  • 1
  • 14
  • 33
rinomau
  • 1,238
  • 8
  • 10
31

For Laravel 5, you can use artisan command

php artisan route:list instead of php artisan routes.

berkayk
  • 2,376
  • 3
  • 21
  • 26
31

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

Darshan Gada
  • 583
  • 5
  • 9
28

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>";
});
FooBar
  • 5,752
  • 10
  • 44
  • 93
jeanfrg
  • 2,366
  • 2
  • 29
  • 40
14
//Laravel >= 5.4

//Controller index()
$app = app();
$routes = $app->routes->getRoutes();
return view ('Admin::routes.index',compact('routes'));

//view
<table id="routes-table" class="table table-bordered table-responsive">
       <thead>
                <tr>
                    <th>uri</th>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Method</th>
                </tr>
       </thead>
       <tbody>
                @foreach ($routes as $route )
                    <tr>
                        <td>{{$route->uri}}</td>
                        <td>{{$route->getName()}}</td>
                        <td>{{$route->getPrefix()}}</td>
                        <td>{{$route->getActionMethod()}}</td>
                    </tr>
                @endforeach
        </tbody>
</table>
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
Carlos Andrade
  • 181
  • 1
  • 3
  • 1
    Can you please [edit] your answer and add a short explanation about what it does and how it works? Thank you! – Fabio says Reinstate Monica Apr 04 '17 at 16:05
  • @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. – Gideon Maina Aug 18 '17 at 10:18
  • Works for Laravel 5.6.x Thanks – Vicky Gill Aug 09 '18 at 05:05
7

A better way to get it readable is to register a route and print it in web browser with the artisan output directly

Route::get('routes', function() {
     \Artisan::call('route:list');
     return \Artisan::output();
});
Jose Palazuelos
  • 137
  • 2
  • 7
  • 2
    Better change the last line of the Closure to `return '
    ' . \Artisan::output() . '
    ';` for better formatting.
    – parrker9 Jul 22 '15 at 04:12
  • Ány Idea how to filter this? For example for routes which start like this `api/` – utdev Apr 21 '17 at 15:29
  • @utdev I know this is old comments, you can pass the parameter to filter like this `\Artisan::call('route:list', ['--path' => 'api']);` – Meow Kim Aug 10 '20 at 07:48
5

if you have compiled routes like /login/{id} and you want prefix only:

foreach (Route::getRoutes() as $route) {
    $compiled = $route->getCompiled();
    if(!is_null($compiled))
    {
        var_dump($compiled->getStaticPrefix());
    }
}
Luis Pozenato
  • 51
  • 1
  • 1
5

Code

Laravel <= 5.3

/** @var \Illuminate\Support\Facades\Route $routes  */
$routes = Route::getRoutes();
foreach ($routes as $route) {
    /** @var \Illuminate\Routing\Route $route  */
    echo $route->getPath() .  PHP_EOL;
}

Laravel >= 5.4

/** @var \Illuminate\Support\Facades\Route $routes  */
$routes = Route::getRoutes();
foreach ($routes as $route) {
    /** @var \Illuminate\Routing\Route $route  */
    echo $route->uri. PHP_EOL;
}

Artisan

Laravel 4

php artisan routes

Laravel 5

php artisan route:list
pablorsk
  • 3,861
  • 1
  • 32
  • 37
3
$routeList = Route::getRoutes();

foreach ($routeList as $value)
{
    echo $value->uri().'<br>';
}

use Illuminate\Support\Facades\Route;

On Laravel 5.4, it works, 100 %

Felix Schwarz
  • 2,938
  • 4
  • 28
  • 41
Turan Zamanlı
  • 3,828
  • 1
  • 15
  • 23
1

Console command for those who use Oh-my-zsh with Laravel 5 plugin

la5routes
Ilyich
  • 4,966
  • 3
  • 39
  • 27
1

Not all the routes are available all the time.

For example if you want to get the routes from the RouteServiceProvider then you might need to use the booted callback:

    $this->booted(function () {
        dump(Route::getRoutes());
    }
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
0

For Laravel 5.4.* This code works fine.

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>";
});
Afraz Ahmad
  • 5,193
  • 28
  • 38