/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/php/php-507-211-001/main.php
35 строк
1 KB
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
class Router { private array $routes = []; public function add(string $method, string $pattern, callable $handler): void { $this->routes[] = [$method, $pattern, $handler]; } public function dispatch(string $method, string $uri): void { foreach ($this->routes as [$m, $pattern, $handler]) { if ($m !== $method) continue; $pattern = preg_quote($pattern, '#'); $pattern = preg_replace('#\{(\w+)\}#', '(?P<$1>[^/]+)', $pattern); $pattern = '#^' . $pattern . '$#u'; if (preg_match($pattern, $uri, $matches)) { $params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY); $handler($params); return; } } http_response_code(404); echo "Not found"; } } // Использование $router = new Router(); $router->add('GET', '/users/{id}', function ($params) { echo "User ID: " . htmlspecialchars($params['id']); }); $router->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));