Epsilon47
Epsilon47

Reputation: 838

Laravel get routes by method

How can I get all routes in project which have GET method? I have tried:

Route::getRoutes() which gave me all routes but somehow I could not have filtered them by method.

The Route::getRoutes()->routes would be nice to have but routes is protected property and I do not see any getter.

Upvotes: 2

Views: 1429

Answers (2)

larsemil
larsemil

Reputation: 876

The RouteCollection has a method to sort the routes by their method(eg. GET).

You can use it as below to get the GET routes:

Route::getRoutes()->getRoutesByMethod()['GET']

And to get POST routes:

Route::getRoutes()->getRoutesByMethod()['POST']

Upvotes: 2

Teoman Tıngır
Teoman Tıngır

Reputation: 2866

you can create small helper method.

function getRoutesByMethod(string $method){
    $routes = \Route::getRoutes()->getRoutesByMethod();
    return $routes[$method];
}

then use it in your application

$postRoutes = getRoutesByMethod("POST");

Upvotes: 3

Related Questions