Answers
\Artisan::call('route:list');
will not work, as it is written to print the routes list in cli and return 0
in controller.
protected $router;
public function __construct(\Illuminate\Routing\Router $router)
{
$this->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.
0 comments:
Post a Comment
Thanks