
本文介绍如何在 laravel 的 blade 模板中安全、高效地展示全部已注册路由(含 domain、method、uri、middleware、name、action),无需调用 cli 命令,而是通过 router 服务动态提取并结构化数据。
本文介绍如何在 laravel 的 blade 模板中安全、高效地展示全部已注册路由(含 domain、method、uri、middleware、name、action),无需调用 cli 命令,而是通过 router 服务动态提取并结构化数据。
在 Laravel 中,php artisan route:list 是开发调试时查看路由配置的常用命令,但其输出仅限终端,无法直接用于 Web 界面。若需在后台管理页或开发面板中以表格形式动态展示所有路由(如用于文档生成、权限审计或路由监控),必须绕过 CLI 调用(Artisan::call('route:list') 在 Web 请求中无效且不返回结构化数据),转而从 IlluminateRoutingRouter 实例中获取原始路由集合并手动解析。
✅ 推荐实现方式:控制器中预处理路由数据
首先,在控制器中注入 Router 服务,遍历所有注册路由,提取关键字段并标准化格式:
开箱即用的技能链路由引擎。13 条预定义链覆盖搜索、开发、审查、MLOps、法律、创意等场景,三层路由架构(触发词→SAD反馈→DAG编排),recall@10=96.97%。配置驱动(chains.yaml),零代码扩展。pip install skill-weave-chains 一键安装。
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateRoutingRouter;
use IlluminateSupportStr;
class RouteListController extends Controller
{
protected $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function index()
{
$routes = collect($this->router->getRoutes())
->map(fn ($route) => $this->formatRoute($route))
->filter() // 过滤掉无效或匿名闭包路由(如框架内部路由)
->values()
->all();
return view('routes.list', compact('routes'));
}
protected function formatRoute($route)
{
// 跳过未绑定 action 的路由(如 fallback)
if (! $route->getActionName() && ! $route->action['uses'] instanceof Closure) {
return null;
}
return [
'domain' => $route->domain() ?: '*',
'method' => implode('|', $route->methods()),
'uri' => $route->uri(),
'name' => $route->getName() ?: '—',
'action' => $this->resolveActionName($route),
'middleware' => $this->resolveMiddleware($route),
];
}
protected function resolveActionName($route)
{
if ($route->action['uses'] instanceof Closure) {
return 'Closure';
}
$action = $route->getActionName();
return $action ? ltrim($action, '\') : '—';
}
protected function resolveMiddleware($route)
{
return collect($this->router->gatherRouteMiddleware($route))
->map(fn ($m) => is_string($m) ? $m : get_class($m))
->implode('<br>');
}
}? Blade 模板渲染(resources/views/routes/list.blade.php)
使用标准 HTML 表格 + Tailwind 或 Bootstrap 样式即可清晰呈现:
@extends('layouts.app')
@section('content')
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<h1 class="text-2xl font-bold text-gray-900 mb-6">All Registered Routes</h1>
<div class="overflow-x-auto rounded-lg border border-gray-200">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Domain</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Method</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">URI</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Action</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Middleware</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@foreach($routes as $route)
<tr class="hover:bg-gray-50">
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">{{ $route['domain'] }}</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
{{ $route['method'] }}
</span>
</td>
<td class="px-6 py-4 text-sm text-gray-900 font-mono">{{ $route['uri'] }}</td>
<td class="px-6 py-4 text-sm text-gray-500">{{ $route['name'] }}</td>
<td class="px-6 py-4 text-sm text-gray-900 font-mono">{{ Str::limit($route['action'], 60) }}</td>
<td class="px-6 py-4 text-sm text-gray-500">{!! nl2br(e($route['middleware'])) !!}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<p class="mt-4 text-sm text-gray-500">
Total routes: <strong>{{ count($routes) }}</strong> | Generated in real-time from current application state.
</p>
</div>
@endsection⚠️ 注意事项与最佳实践
- 性能敏感场景慎用:$router->getRoutes() 返回全部路由对象,若项目路由数超千级,建议添加缓存(如 Cache::remember('route-list-data', 3600, fn() => ...))。
- 权限控制:该页面应严格限制访问(如仅 local 环境或管理员角色),避免生产环境暴露路由结构。
- 中间件解析说明:$router->gatherRouteMiddleware($route) 会递归合并路由级、控制器级、全局中间件,确保完整性;闭包中间件显示为 Closure,类中间件显示完整类名。
- URI 与命名空间处理:$route->uri() 已自动处理 Route::prefix() 和 Route::name() 前缀,无需额外拼接;$route->getActionName() 可能为 NULL(如闭包路由),需判空处理。
- 扩展性提示:如需导出 CSV/Excel,可基于 $routes 数组直接构造响应,无需重复解析。
通过此方案,你获得的是完全可控、可定制、与 Laravel 路由系统深度集成的动态路由视图——既复刻了 route:list 的核心信息,又赋予 Web 层灵活展示与交互能力。


















