Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel Routes
<?php
use Illuminate\Support\Facades\Route;
function matches($patterns, $subject)
{
if(! is_array($patterns)) {
return preg_match($patterns, $subject);
}
$isMatched = false;
foreach ($patterns as $pattern) {
if (preg_match($pattern, $subject)) {
$isMatched = true;
break;
}
}
return $isMatched;
}
$routes = (function() {
return collect(Route::getRoutes())
->filter(function ($route) {
return ! matches([
'/^_debugbar/',
'/^_ignition/',
'/^schematics/',
'/^telescope/',
], $route->uri);
})
->filter(function ($route) {
return ! matches('/^generated::/', $route->getName());
})
->reduce(function ($jsRoutes, $route) {
$jsRoutes[$route->getName()] = $route->uri;
return $jsRoutes;
}, []);
})();
function getRouteMiddleware($route)
{
return collect($route->gatherMiddleware())->map(function ($middleware) {
return $middleware instanceof Closure ? 'Closure' : $middleware;
})->implode(', ');
}
$routes = (function() {
return collect(Route::getRoutes())
->filter(function ($route) {
return ! matches([
'/^_debugbar/',
'/^_ignition/',
'/^schematics/',
'/^telescope/',
], $route->uri);
})
->map(function ($route) {
$name = $route->getName();
if($name && matches('/^generated::/', $name)) {
$name = '';
}
return [
// 'host' => $route->action['where'],
'uri' => $route->uri,
'name' => $name, // $route->action['as'] ?? '',
'methods' => $route->methods,
'action' => $route->action['controller'] ?? 'Closure',
'middleware' => getRouteMiddleware($route),
];
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment