我希望找到一种方法,用Laravel 4中的注册路由路径创建一个数组。
本质上,我希望得到这样的列表:
/
/login
/join
/password
我确实遇到了一种方法Route::getRoutes()
,该方法返回一个带有路线信息和资源的对象,但是路径信息受到保护,因此我无法直接访问该信息。
还有其他方法可以实现这一目标吗?也许是另一种方法?
Route::getRoutes()
返回RouteCollection
。在每个元素上,您都可以执行简单的$route->getPath()
来获取当前路线的路径。
每个受保护的参数都可以使用标准的getter获取。
循环工作如下:
$routeCollection = Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
我得到了漂亮的阵列,请尝试
$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']);
}
}
对于将Oh-my-zsh与Laravel 5 plugin一起使用的用户的控制台命令>
la5routes
对于Laravel 5.4。*此代码工作正常。
您可以使用控制台命令:
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.
对于Laravel 5,您可以使用artisan命令
[php artisan route:list
而不是php artisan 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>
使其更具可读性的更好方法是注册一条路线,并直接在artsan输出中在Web浏览器中打印它
Route::get('routes', function() {
\Artisan::call('route:list');
return \Artisan::output();
});
如果您已经编译了/ login / {id}之类的路由,并且只需要前缀:
foreach (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%