How to get a list of registered route paths in Lar

2019-01-10 20:17发布

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?

11条回答
Anthone
2楼-- · 2019-01-10 20:48

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

la5routes
查看更多
姐就是有狂的资本
3楼-- · 2019-01-10 20:49
//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>
查看更多
我命由我不由天
4楼-- · 2019-01-10 20:56

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();
});
查看更多
smile是对你的礼貌
5楼-- · 2019-01-10 21:00

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
查看更多
趁早两清
6楼-- · 2019-01-10 21:01

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>";
});
查看更多
登录 后发表回答