Laravel不承认特质

Laravel不承认特质

问题描述:

I'm changing AuthenticatesUsers.php to use google recaptcha in postLogin method.

Have a trait

<?php

namespace App\Traits;

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

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

and my AuthenticatesUsers.php starts with

<?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;
...
}

In login page, I get this error

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

Can't understand why. In PhpStorm when importing class CaptchaTrait it automatically import App\Traits\CaptchaTrait to AuthenticatesUsers.php

What am I missing?

我正在更改 AuthenticatesUsers.php strong>以在 postLogin 方法。 p>

有特征 p>

 &lt;?php 
 
namespace App \ Traits; 
 
use照亮 \ Support \ Facades \ Input; 
use ReCaptcha \ ReCaptcha; 
 
trait CaptchaTrait {
 public function captchaCheck()
 {
 ... 
} 
} 
  code>  pre>  
 
 

我的 AuthenticatesUsers.php code>以 p>

 &lt;?php 
 
namespace Illuminate \ Foundation \ Auth; \开头 n 
use App \ Traits \ CaptchaTrait; 
use Illuminate \ Http \ Request; 
use Illuminate \ Support \ Facades \ Auth; 
use Illuminate \ Support \ Facades \ Lang; 
 
trait AuthenticatesUsers 
 {
使用RedirectsUsers;  
使用CaptchaTrait; 
 ... 
} 
  code>  pre> 
 
 

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

  FatalErrorException:未找到Trait 
'App \ Traits \ CaptchaTrait 
  code>  pre> 
 
 

无法理解原因。 在PhpStorm导入类 CaptchaTrait strong>时,它会自动将 App \ Traits \ CaptchaTrait strong>导入 AuthenticatesUsers.php strong> p>

我错过了什么? p> div>

Look at your composer.json file for more information on what the App namespace is. It's a link to the app/ directory (PSR-4).

So the namespace App\Traits is equivalent to the directory app/Traits, not app/App/Traits. The namespace and directories have to match what is defined in your composer autoloader, otherwise, it won't know how to load the class/trait/interface.