我希望找到一种方法来创建一个包含Laravel 4中注册路径路径的数组。
基本上,我希望得到一个像这样的列表:
/
/login
/join
/password
我确实遇到了一个方法Route::getRoutes()
,它返回一个带有路由信息和资源的对象,但路径信息受到保护,我无法直接访问这些信息。
还有其他方法可以达到这个目的吗?也许是一种不同的方法?
答案 0 :(得分:95)
Route::getRoutes()
返回RouteCollection
。在每个元素上,您可以执行简单的$route->getPath()
来获取当前路径的路径。
每个受保护的参数都可以使用标准的getter获取。
循环工作如下:
$routeCollection = Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
答案 1 :(得分:44)
您可以使用控制台命令:
Laravel 4 有问题
php artisan routes
Laravel 5 更实际
php artisan route:list
助手(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.
答案 2 :(得分:17)
我创建了一条路线,将在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>";
});
答案 3 :(得分:8)
//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>
答案 4 :(得分:4)
让它变得可读的更好方法是注册路线并直接在网页浏览器中使用工匠输出进行打印
Route::get('routes', function() {
\Artisan::call('route:list');
return \Artisan::output();
});
答案 5 :(得分:3)
如果您已经编译了像/ login / {id}这样的路由,并且只需要前缀:
foreach (Route::getRoutes() as $route) {
$compiled = $route->getCompiled();
if(!is_null($compiled))
{
var_dump($compiled->getStaticPrefix());
}
}
答案 6 :(得分:3)
$routeList = Route::getRoutes();
foreach ($routeList as $value)
{
echo $value->uri().'<br>';
}
使用Illuminate \ Support \ Facades \ Route;
在Laravel 5.4上,它有效,100%
答案 7 :(得分:1)
改善@jeanfrg的答案
它有一些不推荐使用的功能。编辑答案时显示错误,因此将其发布在这里。
Laravel 6、7和8
将其放入routes/web.php
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->methods()[0] . "</td>";
echo "<td>" . $value->uri() . "</td>";
echo "<td>" . $value->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
演示:
通过<url>/routes
答案 8 :(得分:0)
使用Oh-my-zsh 3> Laravel 5 plugin的人的控制台命令
la5routes
答案 9 :(得分:0)
对于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='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->methods()[0] . "</td>";
echo "<td>" . $value->uri() . "</td>";
echo "<td>" . $value->getName() . "</td>";
echo "<td>" . $value->getActionName() . "</td>";
echo "</tr>";
}
echo "</table>";
});
答案 10 :(得分:0)
/** @var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** @var \Illuminate\Routing\Route $route */
echo $route->getPath() . PHP_EOL;
}
/** @var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** @var \Illuminate\Routing\Route $route */
echo $route->uri. PHP_EOL;
}
php artisan routes
php artisan route:list
答案 11 :(得分:0)
我得到了漂亮的阵列,请尝试
$routeCollection = json_decode(json_encode(Route::getRoutes()->get(),true),true);
dd($routeCollection);
示例假设您只需要所有已注册的路由名称,则可以这样获取它:-
foreach ($routeCollection as $key => $value) {
if(array_key_exists('as',$value['action'])){
dump($value['action']['as']);
}
}
答案 12 :(得分:0)
并非所有路线都一直可用。
例如,如果您想从 RouteServiceProvider
获取路由,那么您可能需要使用 booted
回调:
$this->booted(function () {
dump(Route::getRoutes());
}