I’m encountering an issue with my PHP project where the Composer autoload functionality is not working correctly. Specifically, I get a "Class ‘App\Controllers\UserController’ not found" error when trying to access routes defined in my application.
I have set up the autoloading in my composer.json file as follows:
"autoload": {
"psr-4": {
"App\\": "src/app/"
}
}
my-php-project/
├── src/
│ └── app/
│ ├── config/
│ ├── controllers/
│ │ └── UserController.php
│ ├── models/
│ │ └── User.php
│ └── views/
├── public/
│ ├── routes.php
│ └── router.php
│ └── index.php
├── vendor/
└── composer.json
Despite running composer dump-autoload and ensuring the classes follow PSR-4 naming conventions, the autoloading does not seem to recognize my UserController class. I’ve confirmed that the file exists and that it contains the correct namespace declaration.
I also checked the output of class_exists('App\Controllers\UserController')
and it returns false
. This indicates that the autoloader is not properly set up or that there might be a misconfiguration somewhere.
Could someone help me troubleshoot this issue? Any insights or suggestions would be greatly appreciated!
What I Tried and What I Expected
I have tried the following steps to resolve the autoloading issue:
-
Run
composer dump-autoload
: I executed this command to regenerate the autoload files. I expected it to include myUserController
class and all other classes in thesrc/app/
directory. -
Verify Namespace Declaration: I checked the namespace declaration in the
UserController.php
file to ensure it matchesnamespace App\Controllers;
. -
Use Full Namespace in Routes: In my
routes.php
file, I am using the full namespace when creating an instance ofUserController
, like this:
$userController = new \App\Controllers\UserController();
Despite all these efforts, I still receive a "Class ‘App\Controllers\UserController’ not found" error when trying to access my routes. I expected that after following these steps, the autoloading would work correctly, but it still fails to recognize the class.
routes.php
<?php
// public/routes.php
require_once __DIR__ . '/../vendor/autoload.php';
use App\Controllers\UserController;
var_dump(class_exists(UserController::class)); // Deve retornar true
die();
return [
'/' => function () {
include_once __DIR__ . '/../app/views/home.php';
},
'/users' => function () {
// // Instanciando UserController corretamente com autoload do Composer
// var_dump(class_exists('App\Controllers\UserController')); // Verifica se a classe está carregada
// die(); // Para a execução aqui
$userController = new \App\Controllers\UserController();
$userController->index();
}
index.php
<?php
include_once 'router.php';
$routes = include_once 'routes.php';
run($_SERVER["REQUEST_URI"], $routes);
UserController.php
<?php
// app/controllers/UserController.php
namespace App\Controllers;
use App\Models\User;
class UserController {
private $userModel;
public function __construct() {
$this->userModel = new User(); // Instancia o modelo de usuário
}
// Listar todos os usuários
public function index() {
$usuarios = $this->userModel->getAll(); // Obtém todos os usuários
include __DIR__ . '/../views/user/index.php';
}
You need to sign in to view this answers
Leave feedback about this