PHP+Laravel+MySQL用户Email注册、登录
时间: 2023-09-09 16:13:06 浏览: 51
首先,你需要创建一个用户表来存储用户的信息,包括用户名、密码和邮箱等。可以使用Laravel自带的迁移工具来创建表。在命令行中输入`php artisan make:migration create_users_table --create=users`,然后打开创建的迁移文件,添加需要的字段。
```
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
```
接下来,需要创建一个用户模型,并将其关联到用户表。在命令行中输入`php artisan make:model User`,然后打开创建的用户模型文件,在类中添加以下代码:
```
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function getEmailForPasswordReset()
{
return $this->email;
}
}
```
然后,你需要创建一个控制器来处理注册和登录逻辑。在命令行中输入`php artisan make:controller AuthController`,然后打开创建的控制器文件,在类中添加以下代码:
```
class AuthController extends Controller
{
public function showRegistrationForm()
{
return view('auth.register');
}
public function register(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
Auth::login($user);
return redirect('/home');
}
public function showLoginForm()
{
return view('auth.login');
}
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|string|email',
'password' => 'required|string',
]);
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect()->intended('/home');
}
return back()->withErrors(['email' => 'Email or password is incorrect.']);
}
public function logout()
{
Auth::logout();
return redirect('/login');
}
}
```
在这个控制器中,我们定义了四个方法:`showRegistrationForm()`用于显示注册表单,`register()`用于处理注册逻辑,`showLoginForm()`用于显示登录表单,`login()`用于处理登录逻辑,`logout()`用于退出登录。
最后,你需要创建两个视图文件,一个用于显示注册表单,一个用于显示登录表单。在`resources/views/auth`目录下分别创建`register.blade.php`和`login.blade.php`视图文件。在这两个文件中,你可以使用Laravel自带的表单生成器来创建表单。
注册表单:
```
<form method="POST" action="{{ route('register') }}">
@csrf
<div>
<label for="name">Name</label>
<div>
<input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus>
</div>
@if ($errors->has('name'))
<span>
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div>
<label for="email">Email</label>
<div>
<input id="email" type="email" name="email" value="{{ old('email') }}" required>
</div>
@if ($errors->has('email'))
<span>
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div>
<label for="password">Password</label>
<div>
<input id="password" type="password" name="password" required>
</div>
@if ($errors->has('password'))
<span>
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div>
<label for="password-confirm">Confirm Password</label>
<div>
<input id="password-confirm" type="password" name="password_confirmation" required>
</div>
</div>
<div>
<div>
<button type="submit">
Register
</button>
</div>
</div>
</form>
```
登录表单:
```
<form method="POST" action="{{ route('login') }}">
@csrf
<div>
<label for="email">Email</label>
<div>
<input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus>
</div>
@if ($errors->has('email'))
<span>
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div>
<label for="password">Password</label>
<div>
<input id="password" type="password" name="password" required>
</div>
@if ($errors->has('password'))
<span>
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div>
<div>
<button type="submit">
Login
</button>
</div>
</div>
</form>
```
这样,你就可以在Laravel中实现用户Email注册和登录了。