PSR-4 Autoload Namespace Mismatch

Class 'App\Controller\HomeController' not found despite file existing at src/Controller/HomeController.php

Quick Answer

The namespace in the class file does not match the PSR-4 autoload mapping in composer.json. Verify the namespace declaration matches the directory structure and autoload configuration.

Why This Happens

PSR-4 autoloading maps namespaces to directories. If composer.json maps App\ to src/, then App\Controller\HomeController must be in src/Controller/HomeController.php with the namespace App\Controller. A mismatch anywhere in this chain causes the class to not be found.

The Problem

// composer.json: "App\\" => "src/"
// File: src/Controller/HomeController.php
namespace Controllers; // Wrong namespace!

class HomeController {}

The Fix

// composer.json: "App\\" => "src/"
// File: src/Controller/HomeController.php
namespace App\Controller; // Matches PSR-4 mapping

class HomeController {}

Step-by-Step Fix

  1. 1

    Check composer.json autoload

    Look at the autoload.psr-4 section in composer.json to see how namespaces map to directories.

  2. 2

    Verify the namespace

    Ensure the namespace in the PHP file matches what PSR-4 expects: base namespace + subdirectory path.

  3. 3

    Rebuild autoloader

    Run composer dump-autoload -o to regenerate the optimized autoloader after fixing the namespace.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free