如何获得Laravel的注册路径列表?

Kev*_*ung 63 arrays laravel laravel-4 laravel-routing

我希望找到一种方法来创建一个带有Laravel 4中注册路径路径的数组.

从本质上讲,我希望得到一个像这样返回的列表:

/
/login
/join
/password
Run Code Online (Sandbox Code Playgroud)

我确实遇到过一个方法Route::getRoutes(),它返回一个带有路由信息和资源的对象,但路径信息受到保护,我无法直接访问这些信息.

有没有其他方法来实现这一目标?也许是另一种方法?

net*_*n73 109

Route::getRoutes()返回一个RouteCollection.在每个元素上,您可以轻松$route->getPath()获取当前路径的路径.

每个受保护的参数都可以使用标准的getter获取.

循环工作如下:

$routeCollection = Route::getRoutes();

foreach ($routeCollection as $value) {
    echo $value->getPath();
}
Run Code Online (Sandbox Code Playgroud)

  • 为了防止有人发现它有用,Laravel> = 5.5你可以使用:`$ routes = array_map(function(\ Illuminate\Routing\Route $ route){return $ route-> uri;},(array)Route :: getRoutes() - > getIterator()方法);` (8认同)
  • 如果你想避免使用Facades,你可以注入`Illuminate\Routing\Router`. (3认同)

rin*_*mau 48

您可以使用console命令:

Laravel 4 为问的问题

php artisan routes
Run Code Online (Sandbox Code Playgroud)

Laravel 5 更实际

php artisan route:list
Run Code Online (Sandbox Code Playgroud)


助手(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.
Run Code Online (Sandbox Code Playgroud)

  • 现在是`php artisan route:list` (29认同)

ber*_*ayk 28

对于Laravel 5,您可以使用artisan命令

php artisan route:list而不是php artisan routes.


jea*_*frg 18

我创建了一个路由,将在html表中列出每个路由及其各自的详细信息.

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>";
});
Run Code Online (Sandbox Code Playgroud)


小智 9

//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>
Run Code Online (Sandbox Code Playgroud)


Jos*_*los 6

让它可读的更好方法是注册一个路由并直接在 web 浏览器中使用 artisan 输出打印它

Route::get('routes', function() {
     \Artisan::call('route:list');
     return \Artisan::output();
});
Run Code Online (Sandbox Code Playgroud)

  • 最好将 Closure 的最后一行更改为 `return '&lt;pre&gt;' 。\Artisan::output() 。'&lt;/pre&gt;';` 以获得更好的格式。 (2认同)

小智 6

改进@jeanfrg 的回答

它有一些不推荐使用的功能。它在编辑答案时显示错误,因此在此处发布。

Laravel 6、7 和 8

放在里面 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>";
});
Run Code Online (Sandbox Code Playgroud)

演示: 通过访问它<url>/routes

输出演示


小智 5

如果您已经编译了像 /login/{id} 这样的路由并且您只需要前缀:

foreach (Route::getRoutes() as $route) {
    $compiled = $route->getCompiled();
    if(!is_null($compiled))
    {
        var_dump($compiled->getStaticPrefix());
    }
}
Run Code Online (Sandbox Code Playgroud)


pab*_*rsk 5

代码

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

工匠

拉拉维尔 4

php artisan routes
Run Code Online (Sandbox Code Playgroud)

拉拉维尔 5

php artisan route:list
Run Code Online (Sandbox Code Playgroud)