I connect files:
<?php require_once 'app/core/routeconfig.php'; require_once 'app/core/route.php'; $route = new Route; $route -> Run(); ?> Here is the Route file itself:
class Route{ public $any = '([A-Za-z]+)'; private $num = '([0-9]+)'; public $controller; public $action; public $params = array(); public $route; function getUrl(){ return rtrim($_SERVER['REQUEST_URI'], '/'); } function getRoute(){ $i = 0; foreach ($this->routes as $route){ $pattern = $route['pattern']; $pattern = str_replace(':any', $this->any, $pattern); $pattern = str_replace(':num', $this->num, $pattern); //get uri $uri = $this->getUrl(); if(preg_match('#^'.$pattern.'$#', $uri)){ $this->controller = $route['path']['controller']; $this->action = $route['path']['action']; if(!empty($route[$i]['path']['params'])){ foreach ($route[$i]['path']['params'] as $param => $value) { $params = array($param => $value); } } break; } $i++; } return array($this->controller, $this->action); } public function Run(){ $this->route = $this->getRoute(); $controller = $this->route[0]; $action_name = 'Action_'.$this->route[1]; $controller_name = $this->connectionFile($controller); $controller = new $controller_name; $controller -> $action_name(); } function connectionFile($controller){ $this->controller = $controller; $controller_name = 'Controller_'.$controller; $controller_file = strtolower($controller_name).'.php'; $model_name = 'Model_'.$controller; $model_file = strtolower($model_name).'.php'; if(file_exists('app/controllers/'.$controller_file)){ include 'app/controllers/'.$controller_file; }else{ } if(file_exists('app/models/'.$model_file)){ include 'app/models/'.$model_file; } return $controller_name; } } And here is the routeconfig file
<?php $routes = array( array( 'pattern' => '/show/me/fully', 'path' => array( 'controller' => 'Account', 'action' => 'index', 'params' => array() ) ) ) ?> It turns out that the route does not see the variable with the array, help the beginner)
$routesto the constructor of thenew Route($routes)class .... and in the constructor, if I understand correctly, you need to add the$routesvariable ... and in the constructor you get$this->routes = $routes;...... although of course the routes and route fields in the classroom will somehow look weird to be - Alexey Shimansky$routenot used anywhere else, so it can be thrown out as a field from a class (if not used anywhere else), and inRunyou can replace$route = $this->getRoute(); $controller = $route[0]; $action_name = 'Action_'.$route[1]; .....local variable$route = $this->getRoute(); $controller = $route[0]; $action_name = 'Action_'.$route[1]; .....$route = $this->getRoute(); $controller = $route[0]; $action_name = 'Action_'.$route[1]; .....$route = $this->getRoute(); $controller = $route[0]; $action_name = 'Action_'.$route[1]; .....- Alexey Shimansky