Laravel 通过方法获取路由

Laravel get routes by method

如何获取项目中所有具有 GET 方法的路由?我试过:

Route::getRoutes() 这给了我所有的路线,但不知何故我无法通过方法过滤它们。

如果有 Route::getRoutes()->routes 就好了,但是路线受到保护 属性,我没有看到任何 getter

您可以创建小辅助方法。

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

然后在您的应用程序中使用它

$postRoutes = getRoutesByMethod("POST");

RouteCollection 有一种方法可以根据路由的方法(例如 GET)对路由进行排序。

您可以像下面这样使用它来获取 GET 路由:

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

并获取 POST 路线:

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