Laravel Debug App Routes
We’re all aware of the greatness that the Artisan commands add to Laravel applications. Today I wish to discuss one very useful artisan command and how you can make the best of it.
Command
php artisan route:list
The above command will list routes in your application like
Pitfall
When your content is too long; say the controller actions or route name, the command line can’t represent these in a pretty manner.
Improvement
You can replicate the same command logic to have the same information in a view, enabling you to style the data, make it searchable, filterable, …
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
use Illuminate\Support\Facades\Route; /** * Show application routes. * * Forbidden in production environment. * * @return \Illuminate\View\View */ public function showApplicationRoutes() { if (config('app.log_level') == 'production') { abort(403); } $routes = collect(Route::getRoutes()); $routes = $routes->map(function ($route) { return [ 'host' => $route->action['where'], 'uri' => $route->uri, 'name' => $route->action['as'] ?? '', 'methods' => $route->methods, 'action' => $route->action['controller'] ?? 'Closure', 'middleware' => $this->getRouteMiddleware($route), ]; }); return view('routes', [ 'routes' => $routes, ]); } /** * Get route middleware. * * @param \Illuminate\Routing\Route $route * * @return string */ protected function getRouteMiddleware($route) { return collect($route->gatherMiddleware())->map(function ($middleware) { return $middleware instanceof Closure ? 'Closure' : $middleware; })->implode(', '); } |
Find the full source code implement here.