Answer the question
In order to leave comments, you need to log in
How to correct Router?
Good evening, evaluate a simple router and is it worth adjusting it?
Router class:
<?php
namespace app\system;
class Router
{
private $classConfig;
function __construct()
{
$this->classConfig = new Config; // Класс для работы с конфигурацией
}
public function loadRoutes()
{
$arr = $this->classConfig->getConfigs('routes'); // Вывод роутеров (Ниже пример)
if ($arr) {
foreach ($arr as $key => $value) {
$route = '#^' . $key . '$#';
$this->routes[$route] = $value;
}
}
$url = trim($_SERVER['REQUEST_URI'], '/');
foreach ($this->routes as $route => $params) {
if (preg_match($route, $url, $matches)) {
$this->params = $params;
if (isset($this->params['folder'])) {
$this->params['folder'] = '\\' . ucfirst($this->params['folder']);
}
return true;
}
}
return false;
}
public function runRouter()
{
if ($this->loadRoutes()) {
$path = 'app\controllers' . $this->params['folder'] . '\\' . ucfirst($this->params['controller']) . 'Controller';
$action = $this->params['action'] . 'Action';
if (class_exists($path) and method_exists($path, $action)) {
$controller = new $path($this->params);
$controller->$action();
} else {
echo "<b>" . ucfirst($this->params['controller']) . "Controller</b> or <b>" . $action . "</b> not found.";
return false;
}
} else {
echo "404";
}
}
}
return [
'' => [
'controller' => 'main',
'action' => 'index',
],
'admin' => [
'folder' => 'admin',
'controller' => 'admin',
'action' => 'index',
],
];
Answer the question
In order to leave comments, you need to log in
private $classConfig;
$this->classConfig = new Config;
$arr = $this->classConfig->getConfigs('routes'); // Вывод роутеров (Ниже пример)
if ($arr) {
foreach ($arr as $key => $value) {
$route = '#^' . $key . '$#';
$this->routes[$route] = $value;
}
}
getControllerInfo($requestData);
, which will return a DTO instance like/**
* @psalm-immutable
*/
class ControllerInfo {
public string $controllerClass;
public string $controllerMethod;
/**
* @var string[]
* @psalm-var class-string[]
*/
public array $beforeControllerMiddlewares;
/**
* @param string[] $beforeControllerMiddlewares
* @psalm-param class-string[] $beforeControllerMiddlewares
*/
public function __construct(string $controllerClass, string $controllerMethod, array $beforeControllerMiddlewares = [])
{
$this->controllerClass = $controllerClass;
$this->controllerMethod = $controllerMethod;
$this->beforeControllerMiddlewares = $beforeControllerMiddlewares;
}
}
./vendor/bin/psalm
is better than catching them after running the code, especially in production. $url = trim($_SERVER['REQUEST_URI'], '/');
foreach ($this->routes as $route => $params) {
if (preg_match($route, $url, $matches)) {
$this->params = $params;
if (isset($this->params['folder'])) {
$this->params['folder'] = '\\' . ucfirst($this->params['folder']);
}
return true;
}
}
return false;
echo "<b>" . ucfirst($this->params['controller']) . "Controller</b> or <b>" . $action . "</b> not found.";
return false;
$path = 'app\controllers' . $this->params['folder']
SomeController::class
. Why (why) do I prefer to store routers in files (in a database), if I can automate
Do I need to adjust the code above or will it work?
Which is better: load the controller from the router or load the controller class (checks...) that will load the required controller?
ControllersRegistry->getControllerClass()
, got the controller instance. $args = $this->argumentsResolver->resolve($controller, $method, $request);
$response = $controller->{$action}(... $args)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question