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)

  • You most likely then need to pass your $routes to the constructor of the new Route($routes) class .... and in the constructor, if I understand correctly, you need to add the $routes variable ... 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
  • I did it, but I still can't see this array ( - Igor
  • Well ka show. only not here, but for example in ideone.com - Alexey Shimansky
  • Earned, thanks for the help, added the designer and it all worked, just confusing while with $ this-> - Igor
  • ideone.com/r6LEC8 ..... I'm also looking at $route not used anywhere else, so it can be thrown out as a field from a class (if not used anywhere else), and in Run you 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

2 answers 2

You are trying to walk on the property of the routes object that does not exist ( foreach ($this->routes as $route){ ). There is a completely independent $routes variable.
foreach $routes foreach .

UPDATE :

You can add access to a global variable to a method using the global keyword:

 global $routes; foreach ($routes as $route){ 

But it is more correct to transfer dependent data to the constructor:

  1. Add property for class:

     protected $_routes; 
  2. Register constructor:

     public function __construct($data){ $this->_routes = (array) $data; } 
  3. Call this:

     foreach ($this->_routes as $route){ 
  • Tell me how to do it please) - Igor
  • I removed the this keyword, left just $ routes, but it did not help ( - Igor
  • @Igor, updated the answer. - user207618 pm
  • The globals keyword is not a global variable in classes. - Vanya Avchyan
  • @VanyaAvchyan 1) Not globals , but global . 2) C'mon: ideone.com/F8Pgct Everything works. - user207618
 $route = new Route; $route->route = $routes; $route -> Run(); 

You can pass the $routes the Route class in two ways.

1. Through the constructor

 class Route{ public $any = '([A-Za-z]+)'; private $num = '([0-9]+)'; public $controller; public $action; public $params = array(); private $route; public function __construct($route){ $this->route = $route; } ...... ...... } $route = new Route($routes); 

that i don't advise

2. create a new method - setter, where to pass it

 class Route{ public $any = '([A-Za-z]+)'; private $num = '([0-9]+)'; public $controller; public $action; public $params = array(); private $route; public function setRoute($route){ $this->route = $route; } ...... ...... } 

Client class code:

 <?php require_once 'app/core/routeconfig.php'; require_once 'app/core/route.php'; $routes = array( array( 'pattern' => '/show/me/fully', 'path' => array( 'controller' => 'Account', 'action' => 'index', 'params' => array() ) ) ); $route = new Route; $route->setRoute($routes); $route -> Run(); ?> 
  • I did the first way, but why do not you advise it? Nevertheless works) - Igor
  • It is called good form to access private properties through a setter and a getter. - Vanya Avchyan