问题:我想从我的代码中删除require语句,而是使用自动加载。但是当我这样做时,我发现了一个没有找到错误的类:
结构:
composer.json
index.php
users/user-model.php
users/users-route.php
Composer Json(我运行composer dump-autoload):
....
"autoload": {
"psr-4": {
"User\\": "users/"
}
的index.php:
require_once __DIR__ . '/vendor/autoload.php';
//require ('users/user-model.php'); HERE: Doesn't work when I comment this
$klein = new \Klein\Klein();
require ('users/users-routes.php');
$klein->dispatch();
用户-route.php:
use User\UserModel as user;
$klein->respond( 'GET', '/', function( $request, $response, $service, $app ) {
$tt = new user();
echo $tt::name();
} );
用户model.php:
namespace User;
class UserModel
{
public static function name()
{
return 'bob';
}
}
我应该改变什么,所以我不需要:require(' users / user-model.php'); ?
答案 0 :(得分:1)