命名空间,类不加载

时间:2014-08-17 01:42:36

标签: laravel namespaces composer-php

我是命名空间的新手。使用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"
    }
}

我只包括我认为你需要的东西。需要帮助请叫我。谢谢!

1 个答案:

答案 0 :(得分:2)

composer.json更改psr-4部分中

"psr-4": {
    "Services\\": "app/lib/Services"
}

同时更改

$userCreator = App::make('Services/Users/UserCreator');

$userCreator = App::make('Services\Users\UserCreator');
                                  ^     ^