1

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?

doremi
  • 14,921
  • 30
  • 93
  • 148

1 Answers1

2

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.

Seldaek
  • 40,986
  • 9
  • 97
  • 77
  • Say I wanted to veer away from the psr-0 and just modify my composer.json to get it work with my existing folder structure. How would I do that? – doremi Mar 04 '13 at 00:18
  • Use `classmap` autoloading then, that supports anything. `"autoload": { "classmap": [ "app/" ] }` will scan the entire app directory for .php/.inc files and find all classes defined in any. It's less convenient for developing the package itself though because when you add a new class you should run `composer dump-autoload` again to make it discover it. – Seldaek Mar 04 '13 at 11:51