如何获取Laravel中已注册路线的列表?


77

我希望找到一种使用Laravel 4中注册的路由路径创建数组的方法。

从本质上讲,我正在寻找要返回的列表,例如:

/
/login
/join
/password

我确实遇到了一种方法Route::getRoutes(),该方法返回带有路线信息以及资源的对象,但是路径信息受到保护,因此我无法直接访问该信息。

还有其他方法可以做到这一点吗?也许是另一种方法?


Answers:


119

Route::getRoutes()返回RouteCollection。在每个元素上,您都可以简单$route->getPath()地获取当前路线的路径。

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

循环如下所示:

$routeCollection = Route::getRoutes();

foreach ($routeCollection as $value) {
    echo $value->getPath();
}

您如何遍历集合?
Rajan Rawal 2014年

我可以在自定义助手中使用它吗?
poashoas 2015年

@RajanRawalCollection的实现ArrayAccess,使它们的行为就像数组一样,并可以在中使用foreach
安德鲁·布朗

3
如果您希望避免使用Facades,可以注入Illuminate\Routing\Router
安德鲁·布朗

12
万一有人发现它有用,当Laravel> = 5.5时,您可以使用: $routes = array_map(function (\Illuminate\Routing\Route $route) { return $route->uri; }, (array) Route::getRoutes()->getIterator());
siannone

60

您可以使用控制台命令:

Laravel 4 为问的问题

php artisan routes

Laravel 5 更实际

php artisan route:list


助手(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.

31
php artisan route:list现在是
Marklar

Laravel 5确实如此,但问题在于Laravel 4
Zachary Weixelbaum


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>";
});

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>

1
您能否编辑您的答案并添加简短说明,以说明其作用和作用方式?谢谢!
Fabio说恢复莫妮卡的时间

@FabioTurati我们只是从LaravelgetRoutes()方法获取所有路由,然后将它们发送到template,然后html table在遍历所有路由时最终使用数据创建一个法线。例如,我们正在显示uri e.g /homename e.g home_route您分配和其他人。
基甸缅因州

适用于Laravel 5.6.x的作品感谢
Vicky Gill

4

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

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

4

使其更具可读性的一种更好方法是注册一条路线,并直接在artsan输出中在Web浏览器中打印它

Route::get('routes', function() {
     \Artisan::call('route:list');
     return \Artisan::output();
});

1
最好将Closure的最后一行更改return '<pre>' . \Artisan::output() . '</pre>';为更好的格式。
parrker9

ÁnyIdea如何进行过滤?例如,对于以这种方式开始的路线api/
utdev '17

@utdev我知道这是旧评论,您可以像这样传递参数以进行过滤\Artisan::call('route:list', ['--path' => 'api']);
Meow Kim

2

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;
}

工匠

Laravel 4

php artisan routes

Laravel 5

php artisan route:list

2
$routeList = Route::getRoutes();

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

use Illuminate\Support\Facades\Route;

在Laravel 5.4上,它可以工作,100%



0

对于Laravel 5.4。*此代码可以正常工作。

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>";
});

0

改善@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>";
});

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

输出演示

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.