Php 在Laravel中显示已注册的路线
在这个屏幕广播中:Jeffrey Way演示了他创建的一个命令,并在描述中链接到github。然而,有一个更新说,它现在是烤成的Laravel4核心,但我已经搜索了它没有用 总体思路是列出所有路由以及绑定到它们的操作Php 在Laravel中显示已注册的路线,php,laravel,Php,Laravel,在这个屏幕广播中:Jeffrey Way演示了他创建的一个命令,并在描述中链接到github。然而,有一个更新说,它现在是烤成的Laravel4核心,但我已经搜索了它没有用 总体思路是列出所有路由以及绑定到它们的操作 任何帮助都将不胜感激。您可能仍在使用旧版本的L4 beta版。如果下载新的副本,则在运行php-artisan控制台命令时,将看到该副本的列表: php artisan routes (laravel 4) php artisan route:list (laravel 5)
任何帮助都将不胜感激。您可能仍在使用旧版本的L4 beta版。如果下载新的副本,则在运行
php-artisan
控制台命令时,将看到该副本的列表:
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 | |
等等。我创建了一个路由,它将在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>";
});
Route::get('routes',function()){
$routeCollection=Route::getRoutes();
回声“;
回声“;
回显“HTTP方法”;
回声“路线”;
呼应“相应行动”;
回声“;
foreach($routeCollection作为$value){
回声“;
回显“$value->getMethods()[0]”;
回显“$value->getPath()”;
回显“$value->getActionName()”;
回声“;
}
回声“;
});
您可以使用我的库::
要在控制台中显示Laravel 5+中的所有路线,请执行以下操作:
php artisan route:list
对于Lavarel 5和5+而言,命令为:
php artisan route:list
对于较低版本的Lavarel(低于Lavarel 5),应为:
php artisan routes
我不知道这是否有用,但我会在这里分享 您可以在终端中使用此命令查找Laravel中已注册路由的总数
php artisan route:list | wc -l
根据jeanfrg的回答,在laravel 5.6或更新版本上,使用以下内容:
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>";
});
Route::get('routes',function()){
$routeCollection=Route::getRoutes();
回声“;
回声“;
回显“HTTP方法”;
回声“路线”;
呼应“相应行动”;
回声“;
foreach($routeCollection作为$value){
回声“;
回显“$value->methods()[0]”;
回显“$value->uri()”;
回显“$value->getActionName()”;
回声“;
}
回声“;
});
更新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>";
});
Route::get('routes',function()){
$routeCollection=Route::getRoutes();
回声“;
回声“;
回显“HTTP方法”;
回声“路线”;
呼应“相应行动”;
回声“;
foreach($routeCollection作为$value){
回声“;
回显“$value->methods()[0]”;
回显“$value->uri()”;
回显“$value->getActionName()”;
回声“;
}
回声“;
});
您可以使用下面的函数执行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(','),
);
});
请查看以下链接以供参考:
或
您可以使用以下代码:
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);
}
}
}
非常感谢,我也很喜欢从你的屏幕上学习!你还打算做更多的事情吗,也许建立一个简单的博客,以测试为重点还是什么?他拥有laracasts.com,令人惊叹的内容。杰弗里韦自己回答了这个问题?天哪。你是说
php artisan route:list
,不是吗?我有一个页面,可以让我在laravel中运行一些代码,所以这很完美。在laravel 5.6上getPath()
方法更改为uri()
,这与荷马3个月前发布的代码完全相同。这不应该是一个新的答案。如果说它已经在5.4中工作了,那么它需要是一个注释,而不是一个答案。