1

Is there a way to get the method(s) that a route responds to by its name? The UrlGenerator (as I would expect by its name in all fairness), only provides the URL given a route name. Is there a service provider that can return both the URL and any methods that the route can respond to? I would prefer not to have to hook directly into the app's route collection if possible.

1 Answer 1

1

I think the best way for you to go about this would be to extend the UrlGenerator. You could then add a new method to return an array of the HTTP methods allowed for a route.

public function getMethodsForRoute($name) { if (! is_null($route = $this->routes->getByName($name))) { return $route->methods(); } throw new InvalidArgumentException("Route [{$name}] not defined."); } 

As an alternative you may be able to get the current route from the router and return them that way. Its slightly less elegant, however. (Note this is untested)

$name = 'route.name'; $router = app('Illuminate\Routing\Router'); if (! is_null($route = $router->getRoutes()->getByName($name))) { $methods = $route->methods(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Would it perhaps be better to add to the applications RouteServiceProvider, so I'm not changing any built in service providers? I can access the Router from there too.
If you're extending the UrlGenerator I would suggest creating a new service provider that registers your extended version of the UrlGenerator.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.