无法使用Composer自动加载类 [英] Unable to AutoLoad Class using 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屋!