Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active November 22, 2021 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/69e43d8bc7b69eb6c0c168511d529ce7 to your computer and use it in GitHub Desktop.
Save mtvbrianking/69e43d8bc7b69eb6c0c168511d529ce7 to your computer and use it in GitHub Desktop.
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