Last active
November 22, 2021 14:01
-
-
Save mtvbrianking/69e43d8bc7b69eb6c0c168511d529ce7 to your computer and use it in GitHub Desktop.
Laravel Routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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