我希望找到一种使用Laravel 4中注册的路由路径创建数组的方法。
从本质上讲,我正在寻找要返回的列表,例如:
/
/login
/join
/password
我确实遇到了一种方法Route::getRoutes()
,该方法返回带有路线信息以及资源的对象,但是路径信息受到保护,因此我无法直接访问该信息。
还有其他方法可以做到这一点吗?也许是另一种方法?
Answers:
Route::getRoutes()
返回RouteCollection
。在每个元素上,您都可以简单$route->getPath()
地获取当前路线的路径。
每个受保护的参数都可以使用标准的getter获取。
循环如下所示:
$routeCollection = Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
Collection
的实现ArrayAccess
,使它们的行为就像数组一样,并可以在中使用foreach
。
Illuminate\Routing\Router
。
$routes = array_map(function (\Illuminate\Routing\Route $route) { return $route->uri; }, (array) Route::getRoutes()->getIterator());
您可以使用控制台命令:
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.
php artisan route:list
现在是
我创建了一条路线,该路线将在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>";
});
//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>
getRoutes()
方法获取所有路由,然后将它们发送到template
,然后html table
在遍历所有路由时最终使用数据创建一个法线。例如,我们正在显示uri e.g /home
,name e.g home_route
您分配和其他人。
如果您已经编译了/ login / {id}之类的路由,并且只需要前缀:
foreach (Route::getRoutes() as $route) {
$compiled = $route->getCompiled();
if(!is_null($compiled))
{
var_dump($compiled->getStaticPrefix());
}
}
使其更具可读性的一种更好方法是注册一条路线,并直接在artsan输出中在Web浏览器中打印它
Route::get('routes', function() {
\Artisan::call('route:list');
return \Artisan::output();
});
return '<pre>' . \Artisan::output() . '</pre>';
为更好的格式。
api/
\Artisan::call('route:list', ['--path' => 'api']);
/** @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
对于那些谁使用控制台命令哦,我-的zsh与Laravel 5插件
la5routes
对于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>";
});
改善@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