-2

When I try to load my classes using psr-4 autoloading option, I get this error 'Fatal error: Uncaught Error: Class 'HomeController' not found in C:\xampp\htdocs\gacho\App\Core\Application.php:17 Stack trace: #0 C:\xampp\htdocs\gacho\public\index.php(16): App\Core\Application->__construct() #1 {main} thrown in C:\xampp\htdocs\gacho\App\Core\Application.php on line 17 ' Here is my code structure and code:

code structure:

|gacho
  |- App
     |- Controller
        |- HomeController.php
     |- Core
        |- Application.php
     |- Model
     |- View
  |-public
     |- .htaccess
     |- index.php
  |-vendor
     |- composer
        |- autoload_classmap.php
     |- autoload.php
  |-composer.json

index.php:

<?php

use App\Core\Application;

define('ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR);
define('APP', ROOT . 'App' . DIRECTORY_SEPARATOR);
define('CONTROLLER', ROOT . 'App' . DIRECTORY_SEPARATOR . 'Controller' . 
DIRECTORY_SEPARATOR);
define('VIEW', ROOT . 'App' . DIRECTORY_SEPARATOR . 'View' . 
DIRECTORY_SEPARATOR);
define('MODEL', ROOT . 'App' . DIRECTORY_SEPARATOR . 'Model' . 
DIRECTORY_SEPARATOR);
define('CORE', ROOT . 'App' . DIRECTORY_SEPARATOR . 'Core' . 
DIRECTORY_SEPARATOR);

$modules = [ROOT, APP, CORE, CONTROLLER];

require_once __DIR__ . '\..\vendor\autoload.php';

$app = new Application();

composer.json:

{
 "autoload": {
     "psr-4": {
        "App\\": "App/"
      }
   }
}

Application.php:

<?php
 namespace App\Core;

 use App\Core\Controller;
 use App\Controller\HomeController;

 class Application
 {
    protected $controller = 'HomeController';
    protected $action = 'index';
    protected $params = [];

    public function __construct()
    {
       $this->prepareURL();
       if (file_exists(CONTROLLER. $this->controller . '.php')) {
        $this->controller = new $this->controller;
        if (method_exists($this->controller, $this->action)) {
            call_user_func_array([$this->controller, $this->action], $this->params);
          }
       }
    }

protected function prepareURL()
{
    $request = trim($_SERVER['REQUEST_URI'], '/');
    if (!empty($request)) {
        $url = explode('/', $request);
        $this->controller = isset($url[0]) ? $url[0].'Controller' : 'HomeController';
        $this->action = isset($url[1]) ? $url[1] : 'index';
        unset($url[0], $url[1]);
        $this->params = !empty($url) ? array_values($url) : [];
     }
  }
}

HomeController.php:

<?php
 namespace App\Controller;

 use App\Core\Controller;

 class HomeController extends Controller
 {
    public function index($id= '', $name='')
    {
       $this->view('home\index', [
         'name' => $name,
         'id' => $id
      ]);
      $this->view->page_title = 'Home Page';
      $this->view->render();
    }

    public function users()
    {
       $this->view('home\users', []);
       $this->view->page_title = 'Users';
       $this->view->render();
    }
}
rose
  • 447
  • 5
  • 18
  • Please provide a [mcve]. Also, verify that the steps required to reproduce the behaviour actually work. In particular, take care that the way the autoloader is generated has an influence... BTW: According to the widely-accepted https://www.php-fig.org/psr, your classname `homeController` is invalid. Consider not breaking these conventions, it makes things easier. – Ulrich Eckhardt Feb 04 '19 at 18:10
  • See [how you can debug.](https://stackoverflow.com/a/53359412/1020526) – revo Feb 04 '19 at 18:14
  • @revo I did everything, from that post and it still won't work. Same error. – rose Feb 04 '19 at 19:53

3 Answers3

0

The FQCN is App\Core\Application and the path is app\core\Application.php. Using PSR-4 it will try to load from App\Core\Application.php, but that path does not exist (at least not in a case sensitive file system).

Your best option is to change the directory structure to match the namespaces. They need to match exactly, including case.

Arjan
  • 9,784
  • 1
  • 31
  • 41
  • It's running on MS Windows, telling from one of the error messages. – Ulrich Eckhardt Feb 04 '19 at 18:11
  • Looks like it. Still, path and namespace do not match exactly, and that is the problem. – Arjan Feb 04 '19 at 18:15
  • @Arjan It's runs on Windows 7. I changed every directory to match the namespaces and hit composer-dumpautoload I still get almost the same error, just with changed lower cases ' Fatal error: Uncaught Error: Class 'HomeController' not found in C:\xampp\htdocs\gacho\App\Core\Application.php:17 Stack trace: #0 C:\xampp\htdocs\gacho\public\index.php(16): App\Core\Application->__construct() #1 {main} thrown in C:\xampp\htdocs\gacho\App\Core\Application.php on line 17' Also if it helps when I remove name HomeController from protected $controller in Application.php I dont get error just blank page. – rose Feb 04 '19 at 19:06
-1

Try to run this command: composer update

hbakouane
  • 21
  • 1
  • 5
-1

Please Check your .env file and database

Rajib Bin Alam
  • 353
  • 1
  • 4
  • 16