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?

1 Answer 1

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.

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

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.