Doesn't see class. Routing?

Greetings to all. I have here such here an error:
Fatal error: Class 'SiteController' not found in C:\OpenServer\domains\MVC\engine\components\Router.php on line 59


index.php
//connect the router
 use engine\components\Router;

 //get the path C:\OpenServer\domains\MVC\
 define('ROOT', dirname(__DIR__));

 // connect the file functions in which the function startup
 require_once '../engine/config/functions.php';

 // Create a router object and call the method where the error popped up =)
 $router = new Router;
 $router->run();


functions.php
/**
 * Function avtomaticheskoe class loading
*/
spl_autoload_register(function ($class) {
 $file = ROOT . '/' . $class . '.php';

 if (is_file($file)) {
 require_once $file;
}
});


routes.php Here are the routes

$routes = [

 "=> 'site/index',
];

return $routes;


Class Router.php
namespace engine\components;

class Router
{

/**
 * A property to hold an array of ranting
 * @var array
*/
 private $routes = [];

/**
 * Designer
*/
 public function __construct()
{
 $routesPath = ROOT . '/engine/config/routes.php';

 $this->routes = include($routesPath);
}

/**
 * Returns the query string
*/
 public function getUri(){
if(!empty($_SERVER['REQUEST_URI'])){
 return trim($_SERVER['REQUEST_URI'], '/');
}
}

 public function run(){

 $uri = $this->getUri();
// Check the presence of such a query in an array of routes (routes.php)
 foreach ($this->routes as $uriPattern => $route ){

/ Compare $uriPattern $uri
 if (preg_match("~$uriPattern~", $uri)) {

// Get the internal path of the external rule.
 $internalRoute = preg_replace("~$uriPattern~", $route, $uri);

 // Define controller, action, parameters
 $segments = explode('/', $internalRoute);

 $controllerName = ucfirst(array_shift($segments)) . 'Controller';

 $actionName = 'action' . ucfirst(array_shift($segments));

 // To include the class file controller
 $controllerFile = ROOT . 'engine/app/controllers/' . $controllerName . '.php';

 $parameters = $segments;

 if (file_exists($controllerFile)) {
include_once($controllerFile);
}
// Create the object to call the method (i.e. action)
 $controllerObject = new $controllerName;

 /* Call the necessary method ($actionName) at a certain 
 * class ($controllerObject) with the set ($parameters) parameters
*/
 $result = call_user_func_array(array($controllerObject, $actionName), $parameters);

 if ($result != null) {
break;
}
}
}
}
}


It turns out that the controller has

$controllerName = ucfirst(array_shift($segments)) . 'Controller';

var_dump($controllerName );


string(14) "SiteController"

The path to the correct controller:

$controllerFile = ROOT . 'engine/app/controllers/' . $controllerName . '.php';
var_dump($controllerFil);


string(66) "C:\OpenServer\domains\MVCengine/app/controllers/SiteController.php"


But in this line he does not see the class:

// Create the object to call the method (i.e. action)
 $controllerObject = new $controllerName;


Fatal error: Class 'SiteController' not found in C:\OpenServer\domains\MVC\engine\components\Router.php on line 61


Project structure:
59d4fdd94523c126561247.jpeg
June 14th 19 at 20:06

Find more questions by tags PHP