Fix LESS import error and refactor project structure
This commit is contained in:
@@ -2,17 +2,11 @@
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Router - маршрутизатор запросов
|
||||
*/
|
||||
class Router
|
||||
{
|
||||
private array $routes = [];
|
||||
private array $params = [];
|
||||
|
||||
/**
|
||||
* Добавить маршрут
|
||||
*/
|
||||
public function add(string $method, string $route, string $controller, string $action): self
|
||||
{
|
||||
$this->routes[] = [
|
||||
@@ -24,25 +18,16 @@ class Router
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET маршрут
|
||||
*/
|
||||
public function get(string $route, string $controller, string $action): self
|
||||
{
|
||||
return $this->add('GET', $route, $controller, $action);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST маршрут
|
||||
*/
|
||||
public function post(string $route, string $controller, string $action): self
|
||||
{
|
||||
return $this->add('POST', $route, $controller, $action);
|
||||
}
|
||||
|
||||
/**
|
||||
* Найти маршрут по URL и методу
|
||||
*/
|
||||
public function match(string $url, string $method): ?array
|
||||
{
|
||||
$method = strtoupper($method);
|
||||
@@ -57,7 +42,6 @@ class Router
|
||||
$pattern = $this->convertRouteToRegex($route['route']);
|
||||
|
||||
if (preg_match($pattern, $url, $matches)) {
|
||||
// Извлекаем параметры из URL
|
||||
$this->params = $this->extractParams($route['route'], $matches);
|
||||
|
||||
return [
|
||||
@@ -71,27 +55,16 @@ class Router
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Преобразовать маршрут в регулярное выражение
|
||||
*/
|
||||
private function convertRouteToRegex(string $route): string
|
||||
{
|
||||
$route = trim($route, '/');
|
||||
|
||||
// Заменяем {param} на regex группу
|
||||
$pattern = preg_replace('/\{([a-zA-Z_]+)\}/', '([^/]+)', $route);
|
||||
|
||||
return '#^' . $pattern . '$#';
|
||||
}
|
||||
|
||||
/**
|
||||
* Извлечь параметры из совпадений
|
||||
*/
|
||||
private function extractParams(string $route, array $matches): array
|
||||
{
|
||||
$params = [];
|
||||
|
||||
// Находим все {param} в маршруте
|
||||
preg_match_all('/\{([a-zA-Z_]+)\}/', $route, $paramNames);
|
||||
|
||||
foreach ($paramNames[1] as $index => $name) {
|
||||
@@ -103,9 +76,6 @@ class Router
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить query string из URL
|
||||
*/
|
||||
private function removeQueryString(string $url): string
|
||||
{
|
||||
if ($pos = strpos($url, '?')) {
|
||||
@@ -114,17 +84,11 @@ class Router
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить параметры маршрута
|
||||
*/
|
||||
public function getParams(): array
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Диспетчеризация запроса
|
||||
*/
|
||||
public function dispatch(string $url, string $method): void
|
||||
{
|
||||
$match = $this->match($url, $method);
|
||||
@@ -148,8 +112,6 @@ class Router
|
||||
throw new \Exception("Метод {$action} не найден в контроллере {$controllerClass}");
|
||||
}
|
||||
|
||||
// Вызываем метод контроллера с параметрами
|
||||
call_user_func_array([$controller, $action], $match['params']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user