17

在此截屏视频中:https ://tutsplus.com/lesson/displaying-registered-routes/ Jeffrey Way 演示了他创建的命令,并在描述中链接到 github。然而,有一个更新说它现在已经融入了 Laravel 4 核心,但是我已经搜索过它无济于事。

一般的想法是列出所有路由和绑定到它们的操作。

任何帮助,将不胜感激。

4

9 回答 9

62

控制台命令:

php artisan routes (laravel 4)
php artisan route:list (laravel 5)


+--------+----------------------------------------------------+-----------------------+----------------------------------------------+--------------------------------------------------------------------+---------------+
| Domain | URI                                                | Name                  | Action                                       | Before Filters                                                     | After Filters |
+--------+----------------------------------------------------+-----------------------+----------------------------------------------+--------------------------------------------------------------------+---------------+
|        | GET /admin/configuration                           |                       | ConfigurationController@index                | auth, keyMaster:configuration                                      |               |
|        | POST /admin/configuration                          |                       | ConfigurationController@update               | auth, keyMaster:configuration                                      |               |
|        | GET /admin/logs/errors                             |                       | LogsController@errors                        | auth, keyMaster:logs/errors                                        |               |
|        | GET /admin/logs/errors/{name}                      |                       | LogsController@errors                        | auth, keyMaster:logs/errors                                        |               |
|        | DELETE /admin/logs/errors                          |                       | LogsController@delete                        | auth, keyMaster:logs/errors                                        |               |
|        | GET /admin/logs/events                             |                       | LogsController@events                        | auth, keyMaster:logs/events                                        |               |
|        | GET /admin/logs/events/data                        |                       | LogsController@eventsData                    | auth, keyMaster:logs/events                                        |               |

ETC...

于 2013-12-12T15:42:01.783 回答
24

我创建了一个路由,它将在 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='80%'><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->getActionName() . "</td>";
        echo "</tr>";
    }
    echo "</table>";
});
于 2014-05-15T08:49:01.523 回答
11

您可能仍在使用旧版本的 L4 测试版。如果您下载新副本,您会在运行时看到它列出php artisan

于 2013-04-11T17:42:07.290 回答
9

您可以使用我的库:asvae/laravel-api-tester

Laravel api 测试器


要在Laravel 5+的控制台中显示所有路由,请执行以下操作:

php artisan route:list
于 2015-04-08T21:23:23.917 回答
3

对于 Lavarel 5 和 5+ 命令是:

php artisan route:list

对于较低版本的 Lavarel(低于 Lavarel 5),它将是:

php artisan routes
于 2016-01-14T09:44:22.940 回答
3

我不知道这是否有用,但我会在这里分享。

您可以在终端中使用此命令来查找 Laravel 中注册路由的总数。

php artisan route:list | wc -l
于 2018-08-24T03:33:58.630 回答
2

您可以使用以下功能Laravel 5.8

$routes = collect(\Route::getRoutes())
                    ->map(function ($route) { 
                            return  array(
                                            'domain' => $route->domain(),
                                            'method' => implode('|', $route->methods()),
                                            'uri'    => $route->uri(),
                                            'name'   => $route->getName(),
                                            'action' => ltrim($route->getActionName(), '\\'),
                                            'middleware' => collect($route->gatherMiddleware())
                                                            ->map(function ($middleware) {
                                                                return $middleware instanceof Closure ? 'Closure' : $middleware;
                                                            })->implode(','),
                                        ); 
                        });

请查看以下链接以供参考:

https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Console/RouteListCommand.php

或者

您可以使用以下代码:

foreach (\Route::getRoutes()->getRoutes() as $route)
        {
            $action = $route->getAction();
            $uri = $route->uri();

            if (array_key_exists('controller', $action))
            {               
                // You can also use explode('@', $action['controller']); here
                // to separate the class name from the method

                if(isset($action['as']) && !empty($action['as'])){

                    $controller = explode("@",str_replace($action['namespace']."\\","",$action['controller']));

                    $controllers[$controller[0]][$action['as']] = array('router_group' => $action['middleware'], 'function' => $controller[1], 'uri' => $uri);
                }
            }
        }
于 2019-10-03T05:19:58.170 回答
0

在 laravel 5.6 或更高版本上,根据 jeanfrg 的回答,使用这个:

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='80%'><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->getActionName() . "</td>";
        echo "</tr>";
    }
    echo "</table>";
});
于 2018-10-19T06:20:11.633 回答
-2

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='80%'><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->getActionName() . "</td>";
        echo "</tr>";
    }
    echo "</table>";
});
于 2019-01-19T20:37:24.560 回答