php 在 Laravel 中显示已注册的路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15955295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying registered routes in Laravel
提问by Jordan
In this screencast: https://tutsplus.com/lesson/displaying-registered-routes/Jeffrey Way demonstrates a command he created, and links to the github in the description. However there is an update to say that it is now baked into the Laravel 4 core, however I have searched for it to no avail.
在此截屏视频中:https: //tutsplus.com/lesson/displaying-registered-routes/Jeffrey Way 演示了他创建的命令,并在描述中链接到 github。然而,有更新说它现在已经融入 Laravel 4 核心,但是我已经搜索了它,但无济于事。
The general idea is that is lists all routes and the action bind to them.
一般的想法是列出所有路由并将操作绑定到它们。
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by JeffreyWay
You're probably still using an older version of the L4 beta. If you download a fresh copy, you'll see it listed when you run php artisan
.
您可能仍在使用旧版本的 L4 测试版。如果您下载新副本,您会在运行php artisan
.
回答by Igor Parra
Console command:
控制台命令:
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...
等等...
回答by jeanfrg
I created a route that will list each route and its respective details in an html table.
我创建了一个路线,它将在 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>";
});
回答by Yauheni Prakopchyk
You can use my library: asvae/laravel-api-tester:
你可以使用我的库:asvae/laravel-api-tester:
To show all routes in console in Laravel 5+do:
要在Laravel 5+ 的控制台中显示所有路由,请执行以下操作:
php artisan route:list
回答by Kamesh Jungi
For Lavarel 5 and 5+ command is :
对于 Lavarel 5 和 5+ 命令是:
php artisan route:list
For Lower version Lavarel ( Lower than Lavarel 5), it would be :
对于较低版本的 Lavarel(低于 Lavarel 5),它将是:
php artisan routes
回答by Fubar
I don't know this might be helpful or not but I'll just share it here.
我不知道这是否有帮助,但我会在这里分享。
You can use this command in terminal to find total number of registered route in Laravel.
您可以在终端中使用此命令来查找 Laravel 中已注册路由的总数。
php artisan route:list | wc -l
回答by Jaydp
You can use below function for Laravel 5.8
您可以使用以下功能 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(','),
);
});
Please check below link for reference:
请检查以下链接以供参考:
https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Console/RouteListCommand.php
https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Console/RouteListCommand.php
OR
或者
You can use the following code:
您可以使用以下代码:
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);
}
}
}
回答by Homer
ON laravel 5.6 or newer, based on jeanfrg's answer, use this:
在 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>";
});
回答by gmejia13
Update for laravel 5.4
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>";
});