From 51bfd64ef40c903a9d39659ee5defe903476ec63 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Tue, 21 Jun 2022 21:21:26 +0300 Subject: [PATCH] =?UTF-8?q?[+]=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=80=D1=88=D1=80=D1=83=D1=82?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .htaccess | 2 +- core/bootstrap.php | 7 ++- core/lib/Core/Models/Router.class.php | 63 +++++++++++++++++++++++++++ core/routes.php | 33 +++++++++++++- 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 core/lib/Core/Models/Router.class.php diff --git a/.htaccess b/.htaccess index 37ef9c6..cabbfde 100644 --- a/.htaccess +++ b/.htaccess @@ -3,5 +3,5 @@ RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d -RewriteRule (.*) /index.php?route=$1 [QSA,L] +RewriteRule (.*) /index.php?routeQuery=$1 [QSA,L] \ No newline at end of file diff --git a/core/bootstrap.php b/core/bootstrap.php index 6a96c6e..dc5e7ac 100644 --- a/core/bootstrap.php +++ b/core/bootstrap.php @@ -4,6 +4,7 @@ use Core\Helpers\Cache; use Core\Template; use Core\CoreException; + use Core\Models\Router; if (session_status() == PHP_SESSION_NONE) { session_start(); @@ -65,4 +66,8 @@ } } else { $USER = null; - } \ No newline at end of file + } + + // запускаем маршрутизатор, передавая ему запрошенный адрес + Router::execute(); + die(); \ No newline at end of file diff --git a/core/lib/Core/Models/Router.class.php b/core/lib/Core/Models/Router.class.php new file mode 100644 index 0000000..d2518c0 --- /dev/null +++ b/core/lib/Core/Models/Router.class.php @@ -0,0 +1,63 @@ + $callback) { + if (preg_match($pattern, $_SERVER['REQUEST_URI'], $params)) // сравнение идет через регулярное выражение + { + array_shift($params); + return call_user_func_array($callback, array_values($params)); + } + } + + if (in_array(self::ERROR_PAGE, array_keys(self::$routes), true)) { + return self::$routes[self::ERROR_PAGE](); + } else { + return self::notFound(); + } + } + + } \ No newline at end of file diff --git a/core/routes.php b/core/routes.php index f0d879c..d262215 100644 --- a/core/routes.php +++ b/core/routes.php @@ -1,2 +1,33 @@