Hello. Part of the URL is duplicated when clicking on links other than the root. My site is not located in the root directory, but in a separate folder.
Here is the address of the main page:
http: // localhost / testTaskForCodeIt /
Here I go to the registration page for the first time:
http: // localhost / testTaskForCodeIt / user / register (so far, so good)
Here I press the registration button again:
http: // localhost / testTaskForCodeIt / user / user / register
And here the user part begins to be duplicated.
.htacces
AddDefaultCharset utf-8 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,QSA] file with routes:
return array( 'user/register' => 'user/register', 'user/login' => 'user/login', 'user/logout' => 'user/logout', '' => 'application/index', ); Here is the router itself:
<?php class Router { private $routes; public function __construct() { $routesPath = ROOT . '/config/routes.php'; $this->routes = include($routesPath); } /** * Returns request string */ private function getURI() { if (!empty($_SERVER['REQUEST_URI'])) { return trim($_SERVER['REQUEST_URI'], '/'); } } public function run() { // Получить строку запроса $uri = $this->getURI(); // Проверить наличие такого запроса в routes.php foreach ($this->routes as $uriPattern => $path) { // Сравниваем $uriPattern и $uri if (preg_match("~$uriPattern~", $uri)) { //var_dump(strpos($uri, $uriPattern)); // Получаем внутренний путь из внешнего согласно правилу. $internalRoute = preg_replace("~^$uriPattern~", $path, $uri); // Определить контроллер, action, параметры $segments = explode('/', $internalRoute); $controllerName = array_shift($segments) . 'Controller'; $controllerName = ucfirst($controllerName); $actionName = 'action' . ucfirst(array_shift($segments)); $parameters = $segments; // Подключить файл класса-контроллера $controllerFile = ROOT . '/controllers/' . $controllerName . '.php'; if (file_exists($controllerFile)) { include_once($controllerFile); } // Создать объект, вызвать метод (т.е. action) $controllerObject = new $controllerName; $result = call_user_func_array(array($controllerObject, $actionName), $parameters); if ($result) { break; } } } } }
Please advise what can be done in this situation.