laravel 用戶認證簡單示例
阿新 • • 發佈:2019-01-15
return mod 用戶信息 pub post quest space mina 跳轉
一、模型代碼:
實現接口:\Illuminate\Contracts\Auth\Authenticatable
並引入trait:\Illuminate\Auth\Authenticatable
<?php namespace App\Model; use Illuminate\Database\Eloquent\Model; class Manager extends Model implements \Illuminate\Contracts\Auth\Authenticatable { use \Illuminate\Auth\Authenticatable;protected $table = ‘manager‘; }
二、控制器代碼:
public function check(Request $request) { if ($request->method() == ‘POST‘) { //判斷驗證碼是否正確 $this->validate($request, [ ‘captcha‘ => ‘required|captcha‘ ]); } $data = $request->only([‘username‘, ‘password‘]);$data[‘status‘] = 1; //用戶認證 $result = \Auth::guard(‘admin‘)->attempt($data, $request->get(‘online‘)); if ($result) { //成功 跳轉到後臺首頁 return redirect(‘/admin/index/index‘); } else { //失敗 跳轉回登錄也 return redirect(‘/admin/public/login‘)->withErrors([‘loginError‘ => ‘用戶名或密碼錯誤。‘ ]); } } public function logout() { \Auth::guard(‘admin‘)->logout(); return redirect(‘/admin/public/login‘); }
三、認證成功後獲取用戶信息
\Auth::guard(‘admin‘)->user()
laravel 用戶認證簡單示例