This commit is contained in:
2018-10-17 11:14:36 +03:00
parent 75a35947e5
commit 04d60d7e2c
2716 changed files with 431449 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
<?php
class Router{
private $routes;
public function __construct(){
$routerPath = ROOT.'/config/routes.php';
$this->routes = require($routerPath);
}
/*
* return 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){
//сравниваем $urlPattern и $url
if(preg_match("~$uriPattern~", $uri)){
//получаем внутренний путь из внешнего согласно правилу
$internalRoute = preg_replace("~$uriPattern~", $path, $uri);
//определить контлоллер, экшен и параметры
$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);
}
$controllerObject = new $controllerName;
if(!method_exists($controllerObject,$actionName)) {
$er = $uri;
require_once (ROOT . TMPL . 'error.php');
return true;
};
$result = call_user_func_array(array($controllerObject, $actionName),$parameters);
if($result != null){
break;
}
}
}
}
}