在此截屏视频中:https ://tutsplus.com/lesson/displaying-registered-routes/ Jeffrey Way 演示了他创建的命令,并在描述中链接到 github。然而,有一个更新说它现在已经融入了 Laravel 4 核心,但是我已经搜索过它无济于事。
一般的想法是列出所有路由和绑定到它们的操作。
任何帮助,将不胜感激。
控制台命令:
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...
我创建了一个路由,它将在 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>";
});
您可能仍在使用旧版本的 L4 测试版。如果您下载新副本,您会在运行时看到它列出php artisan
。
对于 Lavarel 5 和 5+ 命令是:
php artisan route:list
对于较低版本的 Lavarel(低于 Lavarel 5),它将是:
php artisan routes
我不知道这是否有用,但我会在这里分享。
您可以在终端中使用此命令来查找 Laravel 中注册路由的总数。
php artisan route:list | wc -l
您可以使用以下功能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);
}
}
}
在 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>";
});
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>";
});