|
48 | 48 | * @method Response response() |
49 | 49 | * @method void error(Throwable $e) |
50 | 50 | * @method void notFound() |
| 51 | + * @method void methodNotFound(Route $route) |
51 | 52 | * @method void redirect(string $url, int $code = 303) |
52 | 53 | * @method void json($data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) |
53 | 54 | * @method void jsonHalt($data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) |
@@ -91,6 +92,7 @@ class Engine |
91 | 92 | 'halt', |
92 | 93 | 'error', |
93 | 94 | 'notFound', |
| 95 | + 'methodNotFound', |
94 | 96 | 'render', |
95 | 97 | 'redirect', |
96 | 98 | 'etag', |
@@ -562,6 +564,15 @@ public function _start(): void |
562 | 564 | $params[] = $route; |
563 | 565 | } |
564 | 566 |
|
| 567 | + // OPTIONS request handling |
| 568 | + if ($request->method === 'OPTIONS') { |
| 569 | + $allowedMethods = $route->methods; |
| 570 | + $response->status(204) |
| 571 | + ->header('Allow', implode(', ', $allowedMethods)) |
| 572 | + ->send(); |
| 573 | + return; |
| 574 | + } |
| 575 | + |
565 | 576 | // If this route is to be streamed, we need to output the headers now |
566 | 577 | if ($route->is_streamed === true) { |
567 | 578 | if (count($route->streamed_headers) > 0) { |
@@ -644,7 +655,7 @@ public function _start(): void |
644 | 655 | // Get the previous route and check if the method failed, but the URL was good. |
645 | 656 | $lastRouteExecuted = $router->executedRoute; |
646 | 657 | if ($lastRouteExecuted !== null && $lastRouteExecuted->matchUrl($request->url) === true && $lastRouteExecuted->matchMethod($request->method) === false) { |
647 | | - $this->halt(405, 'Method Not Allowed', empty(getenv('PHPUNIT_TEST'))); |
| 658 | + $this->methodNotFound($lastRouteExecuted); |
648 | 659 | } else { |
649 | 660 | $this->notFound(); |
650 | 661 | } |
@@ -839,6 +850,19 @@ public function _notFound(): void |
839 | 850 | ->send(); |
840 | 851 | } |
841 | 852 |
|
| 853 | + /** |
| 854 | + * Function to run if the route has been found but not the method. |
| 855 | + * |
| 856 | + * @param Route $route - The executed route |
| 857 | + * |
| 858 | + * @return void |
| 859 | + */ |
| 860 | + public function _methodNotFound(Route $route): void |
| 861 | + { |
| 862 | + $this->response()->setHeader('Allow', implode(', ', $route->methods)); |
| 863 | + $this->halt(405, 'Method Not Allowed. Allowed Methods are: ' . implode(', ', $route->methods), empty(getenv('PHPUNIT_TEST'))); |
| 864 | + } |
| 865 | + |
842 | 866 | /** |
843 | 867 | * Redirects the current request to another URL. |
844 | 868 | * |
|
0 commit comments