Kev*_*ung 63 arrays laravel laravel-4 laravel-routing
我希望找到一种方法来创建一个带有Laravel 4中注册路径路径的数组.
从本质上讲,我希望得到一个像这样返回的列表:
/
/login
/join
/password
Run Code Online (Sandbox Code Playgroud)
我确实遇到过一个方法Route::getRoutes(),它返回一个带有路由信息和资源的对象,但路径信息受到保护,我无法直接访问这些信息.
有没有其他方法来实现这一目标?也许是另一种方法?
net*_*n73 109
Route::getRoutes()返回一个RouteCollection.在每个元素上,您可以轻松$route->getPath()获取当前路径的路径.
每个受保护的参数都可以使用标准的getter获取.
循环工作如下:
$routeCollection = Route::getRoutes();
foreach ($routeCollection as $value) {
echo $value->getPath();
}
Run Code Online (Sandbox Code Playgroud)
rin*_*mau 48
您可以使用console命令:
Laravel 4 为问的问题
php artisan routes
Run Code Online (Sandbox Code Playgroud)
Laravel 5 更实际
php artisan route:list
Run Code Online (Sandbox Code Playgroud)
助手(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.
Run Code Online (Sandbox Code Playgroud)
jea*_*frg 18
我创建了一个路由,将在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>";
});
Run Code Online (Sandbox Code Playgroud)
小智 9
//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>
Run Code Online (Sandbox Code Playgroud)
让它可读的更好方法是注册一个路由并直接在 web 浏览器中使用 artisan 输出打印它
Route::get('routes', function() {
\Artisan::call('route:list');
return \Artisan::output();
});
Run Code Online (Sandbox Code Playgroud)
小智 6
改进@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>";
});
Run Code Online (Sandbox Code Playgroud)
演示:
通过访问它<url>/routes
小智 5
如果您已经编译了像 /login/{id} 这样的路由并且您只需要前缀:
foreach (Route::getRoutes() as $route) {
$compiled = $route->getCompiled();
if(!is_null($compiled))
{
var_dump($compiled->getStaticPrefix());
}
}
Run Code Online (Sandbox Code Playgroud)
/** @var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** @var \Illuminate\Routing\Route $route */
echo $route->getPath() . PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
/** @var \Illuminate\Support\Facades\Route $routes */
$routes = Route::getRoutes();
foreach ($routes as $route) {
/** @var \Illuminate\Routing\Route $route */
echo $route->uri. PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
php artisan routes
Run Code Online (Sandbox Code Playgroud)
php artisan route:list
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69262 次 |
| 最近记录: |