Reputation: 168
Unable to determine which routes use a macro.
I've registered a macro in my app service provider and used it on a few routes. I would like to see which routes use this registered macro. When I call hasMacro on any route it returns true on all routes regardless of whether or not the route uses it.
It says in the docs that the hasMacro method returns true if the macro is registered which explains why all routes return true.
Docs: https://laravel.com/api/5.6/Illuminate/Routing/Route.html
Is there a way of determining if a route uses a custom macro? I would be happy with changing the group, namespace or middleware when this macro is called then query for that but so far have been unable to do. Any recommendations?
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.");
});
$graphQlRoutes = collect(Router::getRoutes())->filter(function($route) {
/** @var Route $route */
return $route->hasMacro('graphQL');
});
Upvotes: 1
Views: 582
Reputation: 168
AHA! Easy solution. Inside the macro just set a property and then check for that.
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;
});
Upvotes: 0