Unable to AutoLoad Class using Composer

爱⌒轻易说出口 提交于 2019-12-14 00:29:34

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!