thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

Middleware for OPTIONS HTTP method #224

Closed MontealegreLuis closed 4 years ago

MontealegreLuis commented 5 years ago

Is there a way to create/add a generic middleware to respond to OPTIONS HTTP method.

I made a small test and OPTIONS returns a 405 response for the JsonStrategy

But I don't think there's information about the route that a middleware could use.

philipobenito commented 5 years ago

There isn't unfortunately, exposing that info would require a bigger change than you might think but will consider looking at it for the next major release.

ifennna commented 5 years ago

@MontealegreLuis how have you been handling the OPTIONS requests in the meantime? I assume your use case was handling pre-flight requests.

I've been searching for a clean way to handle them seeing as they're sent for each requested route, and there's no * catchall pattern for urls.

TCB13 commented 5 years ago

I'm also interested in knowing how to handle pre-flight requests globally...

ghost commented 5 years ago

+1

mta59066 commented 5 years ago

Is there a way to run the match method manually and get the list of endpoints a given path matches to? I am interested in using this information to extract the methods that are already mapped and respond with the appropriate Allow header for OPTIONS.

$endpoints['/test/{arg1}'] = ['GET','POST'];
$methods_allowed = $endpoints[matchedendpoint('/test/hello-world')];
philipobenito commented 4 years ago

So I've gone with automatically generating OPTIONS routes (if they haven't been defined), with the handler/controller existing on the Strategy, therefore easily extensible with custom strategies.