无法使用Composer自动加载类 [英] Unable to AutoLoad Class using Composer

查看:250
本文介绍了无法使用Composer自动加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目结构如下:

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

在app/controllers/IndexController.php里面,我有:

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
    }
}

这是我的composer.json:

Here's my composer.json:

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

更新composer.json后,我运行composer.phar update来更新生成的自动加载文件.

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

仅供参考-我没有使用任何类型的MVC框架.这只是我喜欢用于小型项目的自定义轻量级结构.

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?

推荐答案

如果使用psr-0自动加载,则需要遵循psr-0规范.这意味着,如果指定"MyApp": "app/",则MyApp \ Models \ Test类必须位于app/MyApp/Models/Test.php中.

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.

这篇关于无法使用Composer自动加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆