1

I have been trying to use namespaces for the first time in ages and I am running into the below problem. I am currently using Composer for a PSR-4 autoloader and I keep getting the error: Fatal error: Class 'API\Library\Config' not found in C:\wamp64\www\project\src\index.php on line 14

composer.json

"autoload": {
    "psr-4": {
        "API\\": "src",
        "API\\Library\\": "src/Library",
        "API\\Controllers\\": "src/Application/Controllers"
    }
}

src/index.php

namespace API;
include_once('vendor/autoload.php');
use API\Library\Config;
$config = new Config(); //line 18

The Folder layout is as such:

folder layout

2
  • 1
    Did you try "$ composer dump-autoload" to re-generate the autoload file? Commented Nov 23, 2017 at 10:45
  • yes I did, sorry I will update OP to confirm Commented Nov 23, 2017 at 10:45

1 Answer 1

2

Its because src is the parent folder. Ideally vendor would be in the same directory as src.

"autoload": {
    "psr-4": {
        "API\\": "",
        "API\\Library\\": "Library",
        "API\\Controllers\\": "Application/Controllers"
    }
}

Would work, or you should restructure your directories.

Also you can leave out "API\\Library\\": "Library", as it will be picked up by "API\\": "",

Sign up to request clarification or add additional context in comments.

2 Comments

seems to be working, didn't think about the position of vendor being an issue so thank you!
np, easy mistake and we all been there :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.