Fadakar Fadakar - 3 months ago 9
PHP Question

how to get routes thats in a middleware in laravel 5.1

How can get collection of route thats registered in a middleware?
In laravel 4.2 I can get collection of filters for each route like this:

$routeCollection = Route::getRoutes();
$filterName = "Visit";
$routeHasFilter = array();
foreach ($routeCollection as $route){
$filters = $route->beforeFilters();
if (count($filters) >= 1) {
$filters = array_keys($filters);
if (in_array($filterName, $filters))
$routeHasFilter[] = $route;
}
}


How can I find routes in laravel 5.1 with middleware?

Answer

You can do something very similar. Something like this:

$routeCollection = Route::getRoutes();
$middlewareName = "Visit";
$routeHasFilter = [];

foreach ($routeCollection as $route)
{
    $middleware = $route->middleware();
    if (count($middleware) > 0)
    {
        if (in_array($middlewareName, $middleware))
        {
            $routeHasFilter[] = $route;
        }
    }
}
Comments