John Davidson

phpstorm - Class 'BaseController' not found in 'RouteController.php'

0 comments
Message:


I started writing a site and encountered this problem. I created RouteController, which serves for address bar parsing, and created an inherited class to have a binding:


protected $controller;
protected $inputMethod;
protected $outputMethod;
protected $parameters;

Well, generally speaking, the base class, which is inherited by RouteController. But when I just added "extends BaseController" to "class RouteController", it immediately started crashing and outputted the following error:



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



Does anyone know where I might have made a mistake or done something wrong? I've never had a problem with this before.


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'
]
];



And i would like to know, why is this error showing up?



My Answer:
PhpStorm is a popular integrated development environment (IDE) for PHP programming. It provides a range of features to help developers write, edit, and debug PHP code more efficiently. One of the key features of PhpStorm is its support for classes, which are a fundamental concept in object-oriented programming.

In PhpStorm, developers can easily create and manage classes within their PHP projects. They can define properties, methods, and constructors for each class, as well as set access modifiers and other class-specific settings. PhpStorm also provides code completion and refactoring tools to help developers work with classes more effectively.

Overall, PhpStorm's support for classes makes it easier for developers to organize and structure their PHP code in a way that is more maintainable and scalable. This can lead to more efficient development workflows and higher-quality code.

Rate this post

4 of 5 based on 8281 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us