显示Laravel的注册路线

时间:2013-04-11 17:37:43

标签: php laravel

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

一般的想法是列出所有路线并且动作绑定到它们。

任何帮助都将不胜感激。

9 个答案:

答案 0 :(得分:48)

控制台命令:

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

等...

答案 1 :(得分: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>";
});

答案 2 :(得分:10)

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

答案 3 :(得分:9)

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

Laravel api tester


要在 Laravel 5 + 中显示控制台中的所有路线,请执行:

php artisan route:list

答案 4 :(得分:3)

对于Lavarel 5和5+命令是:

php artisan route:list

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

php artisan routes

答案 5 :(得分:2)

我不知道这可能有没有帮助,但我只在这里分享。

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

php artisan route:list | wc -l

答案 6 :(得分: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>";
});

答案 7 :(得分:0)

您可以将以下功能用于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

OR

您可以使用以下代码:

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

答案 8 :(得分:-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>";
});