Добрый день!
В роутере есть метод:
public function Run()
{
$uri = $this -> getURI();
foreach ($this -> routes as $uriPattern => $path) {
if (preg_match("~$uriPattern~", $uri))
{
$rout = explode('/', $path);
// print_r($rout);
$controllerName = array_shift($rout) . 'Controller';
$actionName = array_shift($rout);
$controllerFile = ROOT . "/controllers/{$controllerName}.php";
if (file_exists($controllerFile)) {
include_once($controllerFile);
}
$controllerObject = new Controllers\installAppController;
/*
* нужно вот так как то:
* $controllerObject = new Controllers\$controllerName;
*/
$result = $controllerObject -> $actionName();
if ($result !== null) {
break;
}
}
}
}
Как мне формировать имя класса с учетом
use Calculation\Margin\Controllers;
?
Весь файл
<?php
namespace Calculation\Margin\Components\Router;
require_once ROOT.'/config/routes.php';
use Calculation\Margin\Config\RoutesList;
use Calculation\Margin\Controllers;
class Router
{
private $routes;
public function __construct ()
{
$this -> routes = RoutesList::getRoutes();
}
private function getURI(): string
{
if (!empty($_SERVER['REQUEST_URI'])) {
return trim($_SERVER['REQUEST_URI'], '/');
}
}
public function Run()
{
$uri = $this -> getURI();
foreach ($this -> routes as $uriPattern => $path) {
if (preg_match("~$uriPattern~", $uri))
{
$rout = explode('/', $path);
// print_r($rout);
$controllerName = array_shift($rout) . 'Controller';
$actionName = array_shift($rout);
$controllerFile = ROOT . "/controllers/{$controllerName}.php";
if (file_exists($controllerFile)) {
include_once($controllerFile);
}
$controllerObject = new Controllers\installAppController;
/*
* нужно вот так как то:
* $controllerObject = new Controllers\$controllerName;
*/
$result = $controllerObject -> $actionName();
if ($result !== null) {
break;
}
}
}
}
}