2

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.

5
  • what does Route::getRoutes() give you? could you not filter that result into only get? Apparently it gives you back an array so you could run it through array_filter to get only the ones you want Commented Sep 5, 2018 at 8:55
  • I get a collection: ` RouteCollection {#28 ▼ #routes: array:7 [▼ "GET" => array:124 [▶] "HEAD" => array:124 [▶] "POST" => array:63 [▶] "PUT" => array:1 [▶] "PATCH" => array:1 [▶] "DELETE" => array:21 [▶] "OPTIONS" => array:1 [▶] ] ... from which I would like to get only GET routes Commented Sep 5, 2018 at 8:57
  • in that case maybe Route::getRoutes()['GET'] it's a little hard to read in your comment Commented Sep 5, 2018 at 8:58
  • There is also getRouteByMethod() see this api page laravel.com/api/5.6/Illuminate/Routing/… Commented Sep 5, 2018 at 9:00
  • Couldn't you do the same as what's done in this answer. Get all routes, loop over them and create your own array? Commented Sep 5, 2018 at 9:41

2 Answers 2

3

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"); 
Sign up to request clarification or add additional context in comments.

Comments

2

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'] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.