【问题标题】:Laravel does not recognize traitLaravel 不识别特征
【发布时间】:2015-12-30 13:30:27
【问题描述】:

我正在更改 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

我错过了什么?

【问题讨论】:

  • 你的特征文件在哪里?
  • app/App/Traits/CaptchaTrait.php
  • 但是,修改vendor 文件是一种不好的做法。
  • 谢谢@pespantelis。现在只测试。

标签: php laravel-5 traits


【解决方案1】:

查看您的 composer.json 文件以获取有关 App 命名空间是什么的更多信息。它是指向 app/ 目录 (PSR-4) 的链接。

所以命名空间 App\Traits 等价于目录 app/Traits,而不是 app/App/Traits。命名空间和目录必须与 Composer 自动加载器中定义的内容相匹配,否则,它将不知道如何加载类/特征/接口。

【讨论】:

    猜你喜欢
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2016-12-31
    • 2016-06-04
    • 2019-04-07
    • 2015-03-25
    • 2015-05-24
    相关资源
    最近更新 更多