Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

55 řádky
1.3KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. class Router
  5. {
  6. protected array $routes = [];
  7. public function get(string $path, callable|array|string $handler): Route
  8. {
  9. return $this->add('GET', $path, $handler);
  10. }
  11. public function post(string $path, callable|array|string $handler): Route
  12. {
  13. return $this->add('POST', $path, $handler);
  14. }
  15. public function put(string $path, callable|array|string $handler): Route
  16. {
  17. return $this->add('PUT', $path, $handler);
  18. }
  19. public function patch(string $path, callable|array|string $handler): Route
  20. {
  21. return $this->add('PATCH', $path, $handler);
  22. }
  23. public function delete(string $path, callable|array|string $handler): Route
  24. {
  25. return $this->add('DELETE', $path, $handler);
  26. }
  27. public function add(string $method, string $path, callable|array|string $handler): Route
  28. {
  29. $route = new Route($method, $path, $handler);
  30. $this->routes[] = $route;
  31. return $route;
  32. }
  33. public function match(string $method, string $path): ?Route
  34. {
  35. foreach ($this->routes as $route) {
  36. if ($route->matches($method, $path)) {
  37. return $route;
  38. }
  39. }
  40. return null;
  41. }
  42. }

Powered by TurnKey Linux.