Простой роутинг
<?php
namespace Common;
use Backend;
use Frontend\Controllers;
class Router
{
private $uri = [];
/**
* Router constructor.
*/
public function __construct() {
$route = $this->getController();
$method = $route['method'];
$controller = Logic::get()."\\controllers\\" . $route['controller'];
$obj = '';
if ( class_exists($controller) ) {
$obj = new $controller;
} else {
$this->notFound();
}
if ( method_exists($obj, $method) ) {
if ( empty($route['params']) ) {
$obj->$method();
} else {
if ( !$obj->$method($route['params']) ) {
$this->notFound();
}
}
} else {
$this->notFound();
}
}
/**
* @return mixed
*/
private function getController()
{
$this->uri = explode('/', $_SERVER['REQUEST_URI']);
$array['controller'] = 'index';
$array['method'] = 'index';
$array["params"] = [];
if ( Logic::get() === "frontend") {
if ( $this->uri[1] === "news" && !empty($this->uri[2]) ) {
$array['controller'] = "news";
$array['method'] = "getSingle";
$array["params"] = [ 0 => $this->uri[2] ];
return $array;
}
if ( $this->uri[1] === "catalog" && !empty($this->uri[2]) ) {
$array['controller'] = "catalog";
$array['method'] = "categoryView";
$array["params"] = [ 0 => $this->uri[2] ];
if ( !empty($this->uri[3]) ) {
$array['method'] = "singleView";
$array["params"] = [ 0 => $this->uri[2], 1 => $this->uri[3] ];
}
return $array;
}
if (!empty($this->uri[1])) {
$array['controller'] = $this->uri[1];
}
if (!empty($this->uri[2])) {
$array['method'] = $this->uri[2];
}
}
if (Logic::get() === "backend") {
if (!empty($this->uri[2])) {
$this->uri[2] = strtok($this->uri[2], "?");
$array['controller'] = $this->uri[2];
}
if (!empty($this->uri[3])) {
$this->uri[3] = strtok($this->uri[3], "?");
$array['method'] = $this->uri[3];
}
}
return $array;
}
/**
* 404
*/
public function notFound() {
$controller = Logic::get()."\\controllers\\error";
$obj = new $controller;
$obj->index();
}
}
Что на ваш взгляд нужно убрать/добавить?