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?
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
.
来源:https://stackoverflow.com/questions/15179025/unable-to-autoload-class-using-composer