I wonder is it possible to print all route lists in a Blade, just like the way we see them in terminal after typing php artisan route:list
So in that case we just need a table with these headers:
Domain
Method
URI
Middleware
Name
Action
But I don't mean inserting all the route data in a table and then get results.
I wonder can we print the required information automatically or not?
CodePudding user response:
\Artisan::call('route:list');
will not work, as it is written to print the routes list in cli and return 0
in controller.
public function __construct(protected \Illuminate\Routing\Router $router)
{
}
public function index()
{
$routes = collect($this->router->getRoutes())->map(function ($route) {
return $this->getRouteInformation($route);
})->filter()->all();
dd($routes);
}
protected function getRouteInformation(\Illuminate\Routing\Route $route)
{
return [
'domain' => $route->domain(),
'method' => implode('|', $route->methods()),
'uri' => $route->uri(),
'name' => $route->getName(),
'action' => ltrim($route->getActionName(), '\\'),
'middleware' => $this->get_middleware($route),
'vendor' => $this->isVendorRoute($route),
];
}
protected function isVendorRoute(\Illuminate\Routing\Route $route)
{
if ($route->action['uses'] instanceof \Closure) {
$path = (new \ReflectionFunction($route->action['uses']))->getFileName();
} elseif (is_string($route->action['uses']) && str_contains($route->action['uses'], 'SerializableClosure')) {
return false;
} elseif (is_string($route->action['uses'])) {
if ($this->isFrameworkController($route)) {
return false;
}
$path = (new \ReflectionClass($route->getControllerClass()))->getFileName();
} else {
return false;
}
return str_starts_with($path, base_path('vendor'));
}
protected function isFrameworkController(\Illuminate\Routing\Route $route)
{
return in_array($route->getControllerClass(), [
'\Illuminate\Routing\RedirectController',
'\Illuminate\Routing\ViewController',
], true);
}
protected function get_middleware($route)
{
return collect($this->router->gatherRouteMiddleware($route))->map(function ($middleware) {
return $middleware instanceof \Closure ? 'Closure' : $middleware;
})->implode("\n");
}
You can find this code with more information in /vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php
file.
CodePudding user response:
In your controller
you can get the list of routes using Artisan facade
.
public function showRoutes($request) {
$routes = Artisan::call('route:list');
return view('your_view', compact('routes'));
}