我希望找到一种在 Laravel 4 中创建包含已注册路由路径的数组的方法。
基本上,我想要返回类似于以下列表的内容:
/
/login
/join
/password
我确实找到了一个方法Route::getRoutes()
,它返回一个包含路径信息以及资源的对象,但是路径信息是受保护的,我无法直接访问这些信息。
有没有其他方法可以实现这个功能?也许有不同的方法可以使用?
我希望找到一种在 Laravel 4 中创建包含已注册路由路径的数组的方法。
基本上,我想要返回类似于以下列表的内容:
/
/login
/join
/password
我确实找到了一个方法Route::getRoutes()
,它返回一个包含路径信息以及资源的对象,但是路径信息是受保护的,我无法直接访问这些信息。
有没有其他方法可以实现这个功能?也许有不同的方法可以使用?
Route::getRoutes()
返回一个RouteCollection
。 在每个元素上,您可以使用简单的$route->getPath()
来获取当前路由的路径。
每个受保护的参数都可以使用标准getter方法进行获取。
循环操作的工作方式如下:
$routeCollection = Illuminate\Support\Facades\Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
Collection
实现了 ArrayAccess
接口,使它们可以像数组一样运行,并且可以在 foreach
中使用。 - Andrew BrownIlluminate\Routing\Router
。 - Andrew Brown`$routes = array_map(function (\Illuminate\Routing\Route $route)
{
return $route->uri;
}, (array) Route::getRoutes()->getIterator());`
该代码可以获取 Laravel 应用程序中所有路由的 URI。 - siannone你可以使用控制台命令:
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
命令。 - Marklarphp artisan route:list
命令返回“在 'route' 命名空间中没有定义命令。”的错误信息。有什么建议吗? - feeelaphp artisan route:list
代替php artisan routes
。
改进 @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
进行访问
我创建了一个路由,它可以在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
(例如 /home)、name
(例如 home_route)和其他信息。 - Gideon MainaRoute::get('routes', function() {
\Artisan::call('route:list');
return \Artisan::output();
});
return '<pre>' . \Artisan::output() . '</pre>';
。 - parrker9api/
开头的路由。 - utdev\Artisan::call('route:list', ['--path' => 'api']);
。 - Meow Kimforeach (Route::getRoutes() as $route) {
$compiled = $route->getCompiled();
if(!is_null($compiled))
{
var_dump($compiled->getStaticPrefix());
}
}
/** @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
$routeList = Route::getRoutes();
foreach ($routeList as $value)
{
echo $value->uri().'<br>';
}
use Illuminate\Support\Facades\Route;
Laravel 5.4上,它能够正常工作,100%。