Laravel不承认特质

时间:2015-12-30 13:30:28

标签: php laravel-5 traits

我正在更改 AuthenticatesUsers.php ,以便在 postLogin 方法中使用google recaptcha。

有特质

<?php

namespace App\Traits;

use Illuminate\Support\Facades\Input;
use ReCaptcha\ReCaptcha;

trait CaptchaTrait {
    public function captchaCheck()
    {
        ...
    }
}

我的AuthenticatesUsers.php

开头
<?php

namespace Illuminate\Foundation\Auth;

use App\Traits\CaptchaTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;

trait AuthenticatesUsers
{
    use RedirectsUsers;
    use CaptchaTrait;
...
}

在登录页面中,我收到此错误

FatalErrorException in AuthenticatesUsers.php line 13: Trait
'App\Traits\CaptchaTrait' not found

无法理解为什么。在PhpStorm导入类 CaptchaTrait 时,它会自动将 App \ Traits \ CaptchaTrait 导入 AuthenticatesUsers.php

我错过了什么?

1 个答案:

答案 0 :(得分:3)

查看您的composer.json文件,了解有关App命名空间的更多信息。它是app /目录(PSR-4)的链接。

因此命名空间App \ Traits相当于目录app / Traits,而不是app / App / Traits。命名空间和目录必须与作曲家自动加载器中定义的内容相匹配,否则,它不知道如何加载类/特征/接口。