Class 'BaseController' not found in 'RouteController.php'
Начал писать сайт и столкнулся с такой проблемой. Создал RouteController, который служит за разбор адресной строки и создал наследуемый класс, чтобы была связка:
protected $controller;
protected $inputMethod;
protected $outputMethod;
protected $parameters;
Ну в общем говоря базовый класс,который наследуется RouteController'ом. Но когда я только добавил к "class RouteController" "extends BaseController", он у меня сразу начал ругаться и выводил такую ошибку: PHP Fatal error: Uncaught Error: Class 'Core\Base\Controllers\BaseController' not found in /var/www/Artemiys_Shop/Core/Base/Controllers/RouteController.php:10 Stack trace: #0 {main} thrown in /var/www/Artemiys_Shop/Core/Base/Controllers/RouteController.php on line 10
Может кто знает, где я может быть ошибся или делаю не так? Раньше как то с таким не возникали проблемы.
RouteController.php
<?php
namespace Core\Base\Controllers;
use Core\Base\Exceptions\RouteException;
use Core\Base\Settings\Settings;
use Core\Base\Settings\ShopSettings;
class RouteController extends BaseController
{
static private $_instance;
protected $routes;
private function _clone()
{
}
static public function getInstance()
{
if(self::$_instance instanceof self){
return self::$_instance;
}
return self::$_instance = new self;
}
/**
* @throws RouteException
*/
private function _construct()
{
$address_str = $_SERVER['REQUEST_URI'];
if(strpos($address_str, '/') === strlen($address_str) - 1 && strpos($address_str, '/') !== 0){
$this->redirect(rtrim($address_str, '/'), 301);
}
$path = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], 'index.php'));
if($path === PATH){
$this->routes = Settings::get('routes');
if(!$this->routes) throw new RouteException('Website is under maintenance');
$url = explode('/', substr($address_str, strlen(PATH)));
if($url[0] && $url[0] === $this->routes['admin']['alias']){
array_shift($url);
if($url[0] && is_dir($_SERVER['DOCUMENT_ROOT'] . PATH . $this->routes['plugins']['path'] . $url[0])){
$plugin = array_shift($url);
$pluginSettings = $this->routes['settings']['path'] . ucfirst($plugin . 'Settings');
if(file_exists($_SERVER['DOCUMENT_ROOT'] . PATH . $pluginSettings . '.php')){
$pluginSettings = str_replace('/', '\\', $pluginSettings);
$this->routes = $pluginSettings::get('routes');
}
$dir = $this->routes['plugins']['dir'] ? '/' . $this->routes['plugins']['dir'] . '/' : '/';
$dir = str_replace('//', '/', $dir);
$this->controller = $this->routes['plugins']['path'] . $plugin . $dir;
$hrUrl = $this->routes['plugins']['hrUrl'];
$route = 'plugins';
}else{
$this->controller = $this->routes['admin']['path'];
$hrUrl = $this->routes['admin']['hrUrl'];
$route = 'admin';
}
}else{
$hrUrl = $this->routes['user']['hrUrl'];
$this->controller = $this->routes['user']['path'];
$route = 'user';
}
$this->createRoute($route, $url);
if($url[1]){
$count = count($url);
$key = '';
if(!$hrUrl){
$i = 1;
}else{
$this->parameters['alias'] = $url[1];
$i = 2;
}
for( ; $i < $count; $i++){
if(!$key){
$key = $url[$i];
$this->parameters[$key] = '';
}else{
$this->parameters[$key] = $url[$i];
$key = '';
}
}
}
exit();
}else{
try{
throw new \Exception('Incorrect directory');
}
catch (\Exception $e){
exit($e->getMessage());
}
}
}
private function createRoute($var, $arr){
$route = [];
if(!empty($arr[0])){
if($this->routes[$var]['routes'][$arr[0]]){
$route = explode('/', $this->routes[$var]['routes'][$arr[0]]);
$this->controller .= ucfirst($route[0]. 'Controller');
}else{
$this->controller .= ucfirst($arr[0]. 'Controller');
}
}else{
$this->controller .= $this->routes['default']['controller'];
}
$this->inputMethod = $route[1] ? $route[1] : $this->routes['default']['inputMethod'];
$this->outputMethod = $route[2] ? $route[2] : $this->routes['default']['outputMethod'];
return;
}
}
BaseController.php
<?php
namespace Core\Base\Controllers;
use Core\Base\Exceptions\RouteException;
abstract class BaseController
{
protected $controller;
protected $inputMethod;
protected $outputMethod;
protected $parameters;
/**
* @throws RouteException
*/
public function route() {
$controller = str_replace('/', '\\', $this->controller);
try {
$object = new \ReflectionMethod($controller, 'request');
$args = [
'parameters' => $this->parameters,
'inputMethod' => $this->inputMethod,
'outputMethod' => $this->outputMethod
];
$object->invoke(new $controller, $args);
}
catch (\ReflectionException $e){
throw new RouteException($e->getMessage());
}
}
public function request($args){
}
}
Index.php
<?php
define ('VG_ACCESS', true);
header('Content-Type:text/html;charset=utf-8');
session_start();
require_once 'config.php';
require_once 'Core/Base/Settings/internal_settings.php';
use Core\Base\Exceptions\RouteException;
use Core\Base\Controllers\RouteController;
try{
RouteController::getInstance()->route();
}
catch (RouteException $e) {
exit($e->getMessage());
}
Setting.php
<?php
namespace Core\Base\Settings;
class Settings
{
static private $_instance;
private array $routes = [
'admin' => [
'alias' => 'admin',
'path' => 'Core/Admin/Controllers/',
'hrUrl' => false
],
'settings' => [
'path' => 'Core/Base/Settings/'
],
'plugins' => [
'path' => 'Core/Plugins/',
'hrUrl' => false,
'dir' => false
],
'user' => [
'path' => 'Core/User/Controllers/',
'hrUrl' => true,
'routes' => [
]
],
'default' => [
'controller' => 'IndexController',
'inputMethod' => 'inputData',
'outputMethod' => 'outputData'
]
];
