doremi
doremi

Reputation: 15349

Unable to AutoLoad Class using Composer

I have a project structure that looks like:

app/
app/models/
app/controllers/
app/views/
public/
vendor/
composer.json

Inside of app/controllers/IndexController.php, I have:

require '../vendor/autoload.php';

use MyApp\Models\Test;

class IndexController {

    public function __construct() {
       $t = new Test(); // can't be found
    }
}

Here's my composer.json:

{
  "require": {
    "aws/aws-sdk-php": "*",
  },
  "autoload": {
    "psr-0": {
        "MyApp": "app/"
    }
  }
}

After updating composer.json, I run composer.phar update to update the generated autoload files.

FYI - I'm not using any type of MVC framework. This is just a custom lightweight structure I like to use for small projects.

How do I fix my project so that I can autoloaded classes from my models folder and use them properly in my controllers?

Upvotes: 1

Views: 5059

Answers (1)

Seldaek
Seldaek

Reputation: 42076

If you use psr-0 autoloading, then you need to follow the psr-0 spec. That means, if you specify "MyApp": "app/", the class MyApp\Models\Test must be in app/MyApp/Models/Test.php.

Upvotes: 2

Related Questions