There is a class Route , which dynamically creates objects of the class Controller , based on the address bar ( the same MVC pattern from habr ).
In general, it took me to use namespace 's in my application. But, the question arose how to dynamically create objects in the Route class?
Route class:
<?php use core\controllers as cc; class Route { static function start() { //контроллер и действие по умолчанию $controller_name = 'Main'; $action_name = 'index'; //разбиваем uri на страницы $routes = explode('/', $_SERVER['REQUEST_URI']); //получаем имя контроллера, начинаем с первого, т.к. 0й элемент - dns-адрес хоста if (!empty($routes[1])) { $controller_name = $routes[1]; } //получаем имя экшена if (!empty($routes[2])) { $action_name = $routes[2]; } //добавляем префиксы $model_name = 'Model_' . $controller_name; $controller_name = 'Controller_' . $controller_name; $action_name = 'Action_' . $action_name; //подцепляем файл с классом модели (файла может и не быть) $model_file = strtolower($model_name) . '.php'; $model_path = "application/models/" . $model_file; if (file_exists($model_path)) { include "application/models/" . $model_file; } //подцепляем файл с классом контроллера $controller_file = strtolower($controller_name) . '.php'; $controller_path = "application/controllers/" . $controller_file; if (file_exists($controller_path)) { include "application/controllers/" . $controller_file; } else { /* правильно было бы кинуть здесь исключение, но для упрощения сразу сделаем редирект на страницу 404 */ Route::ErrorPage404(); } //создаем контроллер $controller = new $controller_name; $action = $action_name; if ($controller_name == "Controller_Product") { if ($action != 'Action_index') { $action = "Action_product"; } } if (method_exists($controller, $action)) { //вызываем действие контроллера $controller->$action(); } else { // здесь также разумнее было бы кинуть исключение Route::ErrorPage404(); } if ($controller_name == "Admin") { $controller->$action(); } } static function ErrorPage404() { $host = 'http://' . $_SERVER['HTTP_HOST'] . '/'; header('HTTP/1.1 404 Not Found'); header("Status: 404 Not Found"); header('Location:' . $host . '404'); } } Need a solution how to create objects in this code using a namespace?
Tried to add alias namespace'a to $controller = new $controller_name; but, of course, it was not a success.
And nothing, unfortunately, does not occur.