@@ -21,7 +21,7 @@ class Router
2121 *
2222 * @var array
2323 */
24- protected array $ error_code = [];
24+ protected array $ error_codes = [];
2525
2626 /**
2727 * Define the global middleware
@@ -37,11 +37,6 @@ class Router
3737 */
3838 protected string $ prefix = '' ;
3939
40- /**
41- * @var ?string
42- */
43- protected ?string $ special_method = null ;
44-
4540 /**
4641 * Define the domain constraint for routes
4742 *
@@ -247,7 +242,7 @@ public function route(array $definition): void
247242 unset($ cb ['controller ' ]);
248243 }
249244
250- $ route = $ this ->pushHttpVerb ($ method , $ path , $ cb );
245+ $ route = $ this ->pushMany ($ method , $ path , $ cb );
251246
252247 if (isset ($ definition ['middleware ' ])) {
253248 $ route ->middleware ($ definition ['middleware ' ]);
@@ -261,28 +256,24 @@ public function route(array $definition): void
261256 }
262257
263258 /**
264- * Add other HTTP verbs [PUT, DELETE, UPDATE , HEAD, PATCH]
259+ * Add other HTTP verbs [PUT, DELETE, OPTIONS , HEAD, PATCH]
265260 *
266261 * @param string|array $methods
267262 * @param string $path
268263 * @param callable|array|string $cb
269264 * @return Route
270265 */
271- private function pushHttpVerb (string |array $ methods , string $ path , callable |string |array $ cb ): Route
266+ private function pushMany (string |array $ methods , string $ path , callable |string |array $ cb ): Route
272267 {
273268 $ methods = (array ) $ methods ;
274269
275- if (!$ this ->magic_method ) {
276- return $ this ->routeLoader ($ methods , $ path , $ cb );
277- }
278-
279270 foreach ($ methods as $ key => $ method ) {
280- if ($ this ->magic_method === $ method ) {
281- $ methods [$ key ] = $ this -> magic_method ;
271+ if (in_array ( $ this ->magic_method , [ ' PUT ' , ' DELETE ' , ' PATCH ' ]) && in_array ( $ method, [ ' PUT ' , ' DELETE ' , ' PATCH ' ]) ) {
272+ $ methods [$ key ] = ' POST ' ;
282273 }
283274 }
284275
285- return $ this ->routeLoader ($ methods , $ path , $ cb );
276+ return $ this ->push ($ methods , $ path , $ cb );
286277 }
287278
288279 /**
@@ -293,7 +284,7 @@ private function pushHttpVerb(string|array $methods, string $path, callable|stri
293284 * @param callable|string|array $cb
294285 * @return Route
295286 */
296- private function routeLoader (string |array $ methods , string $ path , callable |string |array $ cb ): Route
287+ private function push (string |array $ methods , string $ path , callable |string |array $ cb ): Route
297288 {
298289 $ methods = (array ) $ methods ;
299290
@@ -361,7 +352,7 @@ public function any(string $path, callable|string|array $cb): Route
361352 {
362353 $ methods = array_map ('strtoupper ' , ['options ' , 'patch ' , 'post ' , 'delete ' , 'put ' , 'get ' ]);
363354
364- return $ this ->pushHttpVerb ($ methods , $ path , $ cb );
355+ return $ this ->pushMany ($ methods , $ path , $ cb );
365356 }
366357
367358 /**
@@ -373,7 +364,7 @@ public function any(string $path, callable|string|array $cb): Route
373364 */
374365 public function get (string $ path , callable |string |array $ cb ): Route
375366 {
376- return $ this ->routeLoader ('GET ' , $ path , $ cb );
367+ return $ this ->push ('GET ' , $ path , $ cb );
377368 }
378369
379370 /**
@@ -385,17 +376,7 @@ public function get(string $path, callable|string|array $cb): Route
385376 */
386377 public function post (string $ path , callable |string |array $ cb ): Route
387378 {
388- if (!$ this ->magic_method ) {
389- return $ this ->routeLoader ('POST ' , $ path , $ cb );
390- }
391-
392- $ method = strtoupper ($ this ->magic_method );
393-
394- if (in_array ($ method , ['DELETE ' , 'PUT ' ])) {
395- $ this ->special_method = $ method ;
396- }
397-
398- return $ this ->pushHttpVerb ($ method , $ path , $ cb );
379+ return $ this ->push ('POST ' , $ path , $ cb );
399380 }
400381
401382 /**
@@ -407,7 +388,11 @@ public function post(string $path, callable|string|array $cb): Route
407388 */
408389 public function delete (string $ path , callable |string |array $ cb ): Route
409390 {
410- return $ this ->pushHttpVerb ('DELETE ' , $ path , $ cb );
391+ if ($ this ->magic_method && strtoupper ($ this ->magic_method ) === 'DELETE ' ) {
392+ return $ this ->post ($ path , $ cb );
393+ }
394+
395+ return $ this ->push ('DELETE ' , $ path , $ cb );
411396 }
412397
413398 /**
@@ -419,7 +404,11 @@ public function delete(string $path, callable|string|array $cb): Route
419404 */
420405 public function put (string $ path , callable |string |array $ cb ): Route
421406 {
422- return $ this ->pushHttpVerb ('PUT ' , $ path , $ cb );
407+ if ($ this ->magic_method && strtoupper ($ this ->magic_method ) === 'PUT ' ) {
408+ return $ this ->post ($ path , $ cb );
409+ }
410+
411+ return $ this ->push ('PUT ' , $ path , $ cb );
423412 }
424413
425414 /**
@@ -431,7 +420,11 @@ public function put(string $path, callable|string|array $cb): Route
431420 */
432421 public function patch (string $ path , callable |string |array $ cb ): Route
433422 {
434- return $ this ->pushHttpVerb ('PATCH ' , $ path , $ cb );
423+ if ($ this ->magic_method && strtoupper ($ this ->magic_method ) === 'PATCH ' ) {
424+ return $ this ->post ($ path , $ cb );
425+ }
426+
427+ return $ this ->push ('PATCH ' , $ path , $ cb );
435428 }
436429
437430 /**
@@ -443,7 +436,7 @@ public function patch(string $path, callable|string|array $cb): Route
443436 */
444437 public function options (string $ path , callable |string |array $ cb ): Route
445438 {
446- return $ this ->pushHttpVerb ('OPTIONS ' , $ path , $ cb );
439+ return $ this ->push ('OPTIONS ' , $ path , $ cb );
447440 }
448441
449442 /**
@@ -456,7 +449,7 @@ public function options(string $path, callable|string|array $cb): Route
456449 */
457450 public function code (int $ code , callable |array |string $ cb ): Router
458451 {
459- $ this ->error_code [$ code ] = $ cb ;
452+ $ this ->error_codes [$ code ] = $ cb ;
460453
461454 return $ this ;
462455 }
@@ -468,7 +461,7 @@ public function code(int $code, callable|array|string $cb): Router
468461 */
469462 public function getErrorCodes (): array
470463 {
471- return $ this ->error_code ;
464+ return $ this ->error_codes ;
472465 }
473466
474467 /**
@@ -483,7 +476,7 @@ public function match(array $methods, string $path, callable|string|array $cb):
483476 {
484477 $ methods = array_map ('strtoupper ' , $ methods );
485478
486- return $ this ->pushHttpVerb ($ methods , $ path , $ cb );
479+ return $ this ->pushMany ($ methods , $ path , $ cb );
487480 }
488481
489482 /**
@@ -503,7 +496,7 @@ public function getRoutes(): array
503496 */
504497 public function getSpecialMethod (): string
505498 {
506- return $ this ->special_method ;
499+ return $ this ->magic_method ;
507500 }
508501
509502 /**
@@ -513,7 +506,7 @@ public function getSpecialMethod(): string
513506 */
514507 public function hasSpecialMethod (): bool
515508 {
516- return !is_null ($ this ->special_method );
509+ return !is_null ($ this ->magic_method );
517510 }
518511
519512 /**
0 commit comments