如何在 Laravel 中获取已注册路线路径的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18394891/
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
How to get a list of registered route paths in Laravel?
提问by Kevin Jung
I was hoping to find a way to create an array with the registered routes paths within Laravel 4.
我希望找到一种方法来创建一个数组,其中包含 Laravel 4 中已注册的路由路径。
Essentially, I am looking to get a list something like this returned:
本质上,我希望得到一个像这样返回的列表:
/
/login
/join
/password
I did come across a method Route::getRoutes()
which returns an object with the routes information as well as the resources but the path information is protected and I don't have direct access to the information.
我确实遇到过一个方法Route::getRoutes()
,它返回一个包含路由信息和资源的对象,但路径信息受到保护,我无法直接访问这些信息。
Is there any other way to achieve this? Perhaps a different method?
有没有其他方法可以实现这一目标?也许是不同的方法?
回答by netvision73
Route::getRoutes()
returns a RouteCollection
. On each element, you can do a simple $route->getPath()
to get path of the current route.
Route::getRoutes()
返回一个RouteCollection
. 在每个元素上,您可以简单$route->getPath()
地获取当前路线的路径。
Each protected parameter can be get with a standard getter.
每个受保护的参数都可以使用标准的 getter 来获取。
Looping works like this:
循环是这样工作的:
$routeCollection = Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
回答by rinomau
You can use console command:
您可以使用控制台命令:
Laravel 4as asked in question
Laravel 4为问的问题
php artisan routes
Laravel 5more actual
Laravel 5更实际
php artisan route:list
Helpers (Laravel 4):
助手(Laravel 4):
Usage:
routes [--name[="..."]] [--path[="..."]]
Options:
--name Filter the routes by name.
--path Filter the routes by path.
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
--no-interaction (-n) Do not ask any interactive question.
--env The environment the command should run under.
回答by berkayk
For Laravel 5, you can use artisan command
对于 Laravel 5,您可以使用 artisan 命令
php artisan route:list
instead of php artisan routes
.
php artisan route:list
而不是php artisan routes
.
回答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='10%'><h4>Name</h4></td>";
echo "<td width='70%'><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->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
回答by Carlos Andrade
//Laravel >= 5.4
//Controller index()
$app = app();
$routes = $app->routes->getRoutes();
return view ('Admin::routes.index',compact('routes'));
//view
<table id="routes-table" class="table table-bordered table-responsive">
<thead>
<tr>
<th>uri</th>
<th>Name</th>
<th>Type</th>
<th>Method</th>
</tr>
</thead>
<tbody>
@foreach ($routes as $route )
<tr>
<td>{{$route->uri}}</td>
<td>{{$route->getName()}}</td>
<td>{{$route->getPrefix()}}</td>
<td>{{$route->getActionMethod()}}</td>
</tr>
@endforeach
</tbody>
</table>
回答by Jose Palazuelos
A better way to get it readable is to register a route and print it in web browser with the artisan output directly
让它可读的更好方法是注册一个路由并直接在 web 浏览器中使用 artisan 输出打印它
Route::get('routes', function() {
\Artisan::call('route:list');
return \Artisan::output();
});
回答by Luis Pozenato
if you have compiled routes like /login/{id} and you want prefix only:
如果你编译了像 /login/{id} 这样的路由并且你只想要前缀:
foreach (Route::getRoutes() as $route) {
$compiled = $route->getCompiled();
if(!is_null($compiled))
{
var_dump($compiled->getStaticPrefix());
}
}
回答by Turan Zamanl?
$routeList = Route::getRoutes();
foreach ($routeList as $value)
{
echo $value->uri().'<br>';
}
use Illuminate\Support\Facades\Route;
On Laravel 5.4, it works, 100 %
在 Laravel 5.4 上,100% 有效
回答by pablorsk
Code
代码
Laravel <= 5.3
Laravel <= 5.3
/** @var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** @var \Illuminate\Routing\Route $route */
echo $route->getPath() . PHP_EOL;
}
Laravel >= 5.4
Laravel >= 5.4
/** @var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** @var \Illuminate\Routing\Route $route */
echo $route->uri. PHP_EOL;
}
Artisan
工匠
Laravel 4
Laravel 4
php artisan routes
Laravel 5
Laravel 5
php artisan route:list
回答by Vipertecpro
I Got Pretty Array By this, Please Try
我得到了漂亮的数组,请尝试
$routeCollection = json_decode(json_encode(Route::getRoutes()->get(),true),true);
dd($routeCollection);
Example suppose you want all the registered route namesonly, you can fetch it like this :-
示例假设您只想要所有已注册的路线名称,您可以像这样获取它:-
foreach ($routeCollection as $key => $value) {
if(array_key_exists('as',$value['action'])){
dump($value['action']['as']);
}
}