Laravel 5.5基于內(nèi)置的Auth模塊實(shí)現(xiàn)前后臺(tái)登陸詳解
前言
本文主要介紹了關(guān)于Laravel 5.5基于內(nèi)置的Auth模塊實(shí)現(xiàn)前后臺(tái)登陸的相關(guān)內(nèi)容,更多關(guān)于Auth模塊的內(nèi)容大家可以參考這篇文章:http://www.dbjr.com.cn/article/121401.htm
下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
方法如下:
步驟1:生成Auth
在項(xiàng)目建立完成后,生成內(nèi)置Auth組件:
php artisan make:auth
步驟2:添加guard
打開(kāi)config\auth.php,為方便前后臺(tái)用戶切換,項(xiàng)目共用Users表
'guards' => [ //... 'admin' => [ 'driver' => 'session', 'provider' => 'users', ], //... ],
步驟3:實(shí)現(xiàn)基類
class AdminController extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; public function __construct() { $this->middleware('auth:admin'); } }
步驟4:實(shí)現(xiàn)后臺(tái)登陸控制器
新建appcontrollersAdminLoginController.php
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/admin'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest:admin')->except('logout'); } /** * 重寫登陸頁(yè)面 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function showLoginForm() { return view('backend.login'); } /** * 重寫退出方法 * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function logout(Request $request) { $this->guard()->logout(); $request->session()->flush(); $request->session()->regenerate(); return redirect('/admin/login'); } /** * 重寫guard認(rèn)證 * @return mixed */ protected function guard() { return Auth::guard('admin'); } }
步驟5:實(shí)現(xiàn)登陸后跳轉(zhuǎn)到不同路徑
app\Middleware\RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { $path = $guard ? '/admin' : '/home'; return redirect($path); } return $next($request); }
步驟6:實(shí)現(xiàn)未通過(guò)認(rèn)證跳轉(zhuǎn)不同登陸頁(yè)
app\Exceptions\Handler.php
/** * 重寫實(shí)現(xiàn)未認(rèn)證用戶跳轉(zhuǎn)至相應(yīng)登陸頁(yè) * @param \Illuminate\Http\Request $request * @param AuthenticationException $exception * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse */ protected function unauthenticated($request, AuthenticationException $exception) { if($request->expectsJson()){ return response()->json(['message' => $exception->getMessage()], 401); }else{ return in_array('admin', $exception->guards()) ? return redirect()->guest('/admin/login') : redirect()->guest('login'); } }
完成
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 基于Laravel Auth自定義接口API用戶認(rèn)證的實(shí)現(xiàn)方法
- Laravel中的Auth模塊詳解
- Laravel5.5中利用Passport實(shí)現(xiàn)Auth認(rèn)證的方法
- laravel容器延遲加載以及auth擴(kuò)展詳解
- 通過(guò)修改Laravel Auth使用salt和password進(jìn)行認(rèn)證用戶詳解
- Laravel 5框架學(xué)習(xí)之用戶認(rèn)證
- Laravel實(shí)現(xiàn)用戶多字段認(rèn)證的解決方法
- Laravel多用戶認(rèn)證系統(tǒng)示例詳解
- Laravel框架Auth用戶認(rèn)證操作實(shí)例分析
相關(guān)文章
Laravel框架實(shí)現(xiàn)點(diǎn)播上傳阿里云功能
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)點(diǎn)播上傳阿里云功能,本文給大家分享一段完整的實(shí)例代碼,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2021-09-09smarty中改進(jìn)truncate使其支持中文的方法
這篇文章主要介紹了smarty中改進(jìn)truncate使其支持中文的方法,涉及針對(duì)Smarty源碼中truncate源文件進(jìn)行函數(shù)功能擴(kuò)展的相關(guān)技巧,需要的朋友可以參考下2016-05-05php面向?qū)ο蠡A(chǔ)詳解【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php面向?qū)ο蠡A(chǔ),結(jié)合星際爭(zhēng)霸游戲案例詳細(xì)分析了PHP面向?qū)ο箢?、繼承、重載、接口等相關(guān)概念與使用技巧,需要的朋友可以參考下2020-01-01PHP依賴倒置(Dependency Injection)代碼實(shí)例
這篇文章主要介紹了PHP依賴倒置(Dependency Injection)代碼實(shí)例本文只提供實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-10-10