Handling the HTTP method OPTIONS

Hi!

I am implementing an API and sometimes the frontend is sending a request with the HTTP method OPTIONS (OPTIONS - HTTP | MDN)

If I understand the specification correctly, you are supposed to return in the header what other methods are supported for that specific endpoint.

My question is if there is a preferred way of handling OPTIONS-requests in CakePHP? I couldn’t find anything in the documentation about that.

Right now I add OPTIONS to the setMethods() function for the endpoint in routes.php and handle the OPTIONS request in the controller action. It works, but it feels like there must be a better way of handling the OPTIONS method in CakePHP.

How do you handle OPTIONS?

Thank you!

(I’m using CakePHP 4.2)

Most of the time OPTIONS method is used for CORS.

Use cakephp-cors plugin or cors-middleware for example

1 Like