我是命名空间的新手。使用laravel。
我的文件夹结构是
app
-lib
-Services
-Users
-UserCreator.php
-controllers
-SiteController.php
composer.json
我的问题是每当我尝试加载它时都会说Class Services/Users/UserCreator does not exist
。
UserCreator.php:
<?php
namespace Services\Users;
class UserCreator {
}
SiteController.php
<?php
use Services\Users\UserCreator;
// more code here...
if ($validator->fails()) {
echo 'fail';
} else {
$userCreator = App::make('Services/Users/UserCreator');
if ($userCreator::create(Input::all())) {
return Redirect::to('/')->with('message', 'Account Created');
} else {
return Redirect::to('/error');
}
}
}
Composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"Services\\Users\\": "app/lib/Services/Users/UserCreator"
}
}
我只包括我认为你需要的东西。需要帮助请叫我。谢谢!
答案 0 :(得分:2)
在composer.json
更改psr-4部分中
"psr-4": {
"Services\\": "app/lib/Services"
}
同时更改
$userCreator = App::make('Services/Users/UserCreator');
到
$userCreator = App::make('Services\Users\UserCreator');
^ ^