0
votes

I am using laravel 6.1 . I am trying do dynamic permission for user for add,edit ,delete and view .so trying to get route list from laravel router using following method

$routes = [];

foreach (Route::getRoutes()->getIterator() as $route){

    $routes[] = $route->uri;

}

This will return all route url list but problem here is for paramter routes list it has below

post-detail/{categoryId}/{postid}

How i can compare in middleware whether url is matching to this route .

Also is it possible to get route type along with route list.

1

1 Answers

1
votes

The Route object has a matches method to see if the request matches the route which can also check the HTTP method:

$route->matches($request, true);

laravel/framework 6.x - Illuminate\Routing\Route@matches

You can also ask the Route Collection to find a matching route for the current Request:

$route = Route::getRoutes()->match($request);

Though this would just be the current route: $request->route()

laravel/framework 6.x - Illuminate\Routing\RouteCollection@match

Though I am not really sure what you are trying to do just by iterating through the routes.


The HTTP methods the Route responds to can be retrieved via $route->methods().

The HTTP method of the current Request can be accessed via $request->method().