user3161917
user3161917

Reputation: 21

Class not found with composer autoload

I have my file structure as

/
/app/
/app/logs/
/app/templates/
/app/index.php
/public_html/
/public_html/.htaccess
/public_html/index.php
/vendor
/vendor/(all vendor here)

My vhost is pointing to /public_html

in app/Index.php

namespace App;

class Index {}

Composer.json

"autoload":
{
    "psr-0":
    {
        "App\\":  "app/"
    }
}

Yet it is still showing as ( ! ) Fatal error: Class 'App\Index' not found in C:\wamp\www\project\public_html\index.php on line 34

Line 34:

new \App\Index();

Using Slimframework as well if that matters, can't think what is wrong

Upvotes: 2

Views: 3400

Answers (1)

Nic Wortel
Nic Wortel

Reputation: 11423

Since you were using the PSR-0 standard, PHP was looking for the file app/App/Index.php, which does not exist. Note that in PSR-0, you define a base directory (app in your case) where the mapped namespace (App) can be found. However, the file structure within that base directory should exactly match the fully qualified class names. So class App\FooBar should be in the file app/App/FooBar.php. Note that app is the base directory, and App is the directory that contains all subdirectories and PHP files for that namespace.

Since this is not the case in your application (and also because PSR-0 has been deprecated), you should (as you already did) use PSR-4, the new autoloading standard, instead. In PSR-4, you can directly map a certain namespace to a certain directory. In your case, you have mapped the App namespace to the app directory, so that PHP will open the app/Index.php file if you need to use the App\Index class.

Upvotes: 2

Related Questions