Routes Group Allowed Methods


#1

Hello,

Is there a way to get the Allowed methods in a group ?

Example:

$this->group(’/test’, function(){

        $this->get('','TestController:get');
        $this->post('','TestController:update');
    });
Inside **TestController.php**:

public function get(Request $request, Response $response, Array $args){

   // Allow : GET,POST
   $allowHeaders = anyway to get this Request group allowed Methods.

  return $response->withHeaders('Allow', implode(',', $allowHeaders));

}

Same with update function.

Is there anyway to do that ?

Thanks,
LosLobos.


#2

I’m not aware of any way to do that on a group. You can do it for a given route callable…

$methods = $request->getAttribute('route')->getMethods();

An alternative might be (I haven’t tried this) to use the map method for building the routes to the controller.

$app->map(['GET', 'POST'], 'TestController');

Then from within your TestController’s __invoke method you would work out which methods are allowed and then call the appropriate controller method.


#3

The map is a good idea but it doesn’t fit in my schema ( My __invoke of controller is already in use ).

I was thinking in making a Middleware to add the Allow Methods Header by looking into how $methods are injected into notAllowedHandler. ( Don’t know if it is possible using it in the middleware trought )

I’ll update here if I get somewhere in this, if its too hard maybe I’ll drop it.