我正在更新到Laravel 5.4,并在尝试显示登录屏幕时收到以下错误消息。
我收到以下错误消息:
Trait' Illuminate \ Foundation \ Auth \ AuthenticatesAndRegistersUsers'找不到
这里是AuthController类:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Where to redirect users after logout.
*
* @var string
*/
protected $redirectAfterLogout = '/login';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => ['getLogout']]);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
答案 0 :(得分:15)
如果您使用的是Laravel 7+,则需要先运行以下命令来安装laravel/ui
软件包(因为它包含auth后端代码):
$composer require laravel/ui
然后
<?php
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
}
答案 1 :(得分:3)
与laravel 5.4我们在这个特性上有一些变化。现在我们有两个不同的特征:
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
如果你安装一个新的5.4 laravel应用程序,你会看到现在你有了LoginController和RegisterController而不是AuthController
答案 2 :(得分:2)
我认为你需要使用这个特性
use Illuminate\Foundation\Auth\AuthenticatesUsers;
答案 3 :(得分:0)
如果您要迁移到 Laravel 8.x,请考虑将您的身份验证层移至 Laravel Breeze:
$ composer require laravel/breeze --dev
$ php artisan breeze:install
有关详细信息,请参阅 Starter Kits。