From e3cd43a9a666230b1d73e345b20dea48b3b4dcff Mon Sep 17 00:00:00 2001 From: Rougin Gutib Date: Fri, 12 Jan 2024 18:37:30 +0800 Subject: [PATCH] Fix issue when capturing routes with OPTIONS --- src/Routing/Dispatcher.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Routing/Dispatcher.php b/src/Routing/Dispatcher.php index 2c341cb2..adf05453 100644 --- a/src/Routing/Dispatcher.php +++ b/src/Routing/Dispatcher.php @@ -15,7 +15,7 @@ class Dispatcher implements DispatcherInterface /** * @var string[] */ - protected $allowed = array('DELETE', 'GET', 'PATCH', 'POST', 'PUT'); + protected $allowed = array('DELETE', 'GET', 'OPTIONS', 'PATCH', 'POST', 'PUT'); /** * @var \Rougin\Slytherin\Routing\RouterInterface @@ -101,7 +101,7 @@ protected function validMethod($method) { if (in_array($method, $this->allowed)) return true; - $message = 'Used method is not allowed'; + $message = 'Used method is not allowed (' . $method . ')'; throw new \BadMethodCallException($message); } @@ -125,7 +125,9 @@ protected function match($method, $uri) $sameMethod = $route->getMethod() === $method; - if ($matched && $sameMethod) + $isOptions = $route->getMethod() === 'OPTIONS'; + + if ($matched && ($sameMethod || $isOptions)) { return $route->setParams($matches); }