Laravel检查路由是否使用宏

时间:2019-02-08 18:22:25

标签: laravel routes router

无法确定哪些路由使用了宏。

我已经在我的应用服务提供商中注册了一个宏,并在一些路由上使用了它。我想看看哪些路由使用了此注册宏。当我在任何路由上调用hasMacro时,无论该路由是否使用它,它都会在所有路由上返回true。

在文档中说,如果注册了宏,则hasMacro方法将返回true,这说明了为什么所有路由都返回true的原因。

文档:https://laravel.com/api/5.6/Illuminate/Routing/Route.html

是否可以确定路线是否使用自定义宏?当调用此宏然后查询该内容时,我很乐意更改组,名称空间或中间件,但到目前为止还无法完成。有什么建议吗?

const
Route::macro('graphQL', function ($type = 'query', $isList = true) {
            if (!in_array($type, ['query', 'mutation'])) throw new InvalidGraphQLTypeException();

            // throw new GraphQLControllerMethodException("This route is for a GraphQL endpoint and can not be accessed.");
        });

1 个答案:

答案 0 :(得分:0)


啊!简单的解决方案。在宏内只需设置一个属性,然后检查该属性即可。

Route::macro('graphQL', function ($type = 'query', $isList = true) {
            if (!in_array($type, ['query', 'mutation'])) throw new InvalidGraphQLTypeException();

            $this->graphQl = true;

            return $this;

            // throw new GraphQLControllerMethodException("This route is for a GraphQL endpoint and can not be accessed.");
        });
$graphQlRoutes = collect(Router::getRoutes())->filter(function($route) {
            return isset($route->graphQl) && $route->graphQl;
        });
相关问题