Use laravel5.5 to implement a function. This article mainly introduces to you the relevant information on how Laravel 5.5 implements front-end and back-end login based on the built-in Auth module. The article introduces it in detail through sample code. I hope it can help you.
The method is as follows:
Step 1: Generate Auth
After the project is established, generate the built-in Auth component:
php artisan make:auth
Step 2: Add guard
Open config\auth.php. In order to facilitate front-end and back-end user switching, the projects share the Users table
'guards' => [ //... 'admin' => [ 'driver' => 'session', 'provider' => 'users', ], //... ],
Step 3: Implement the base class
class AdminController extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; public function __construct() { $this->middleware('auth:admin'); } }
Step 4: Implement background login control Device
New 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'); } /** * 重写登陆页面 * @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认证 * @return mixed */ protected function guard() { return Auth::guard('admin'); } }
Step 5: Jump to different paths after login
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); }
Steps 6: Implementation of jumping to different login pages if authentication fails
app\Exceptions\Handler.php
/** * 重写实现未认证用户跳转至相应登陆页 * @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'); } }
Related recommendations:
How to call front and backend methods in javascript Summary of examples
Package Auto Discovery in Laravel5.5 details introduction
Laravel5.5 new features error reporting and graphic introduction
The above is the detailed content of Laravel 5.5 implements front-end and back-end login. For more information, please follow other related articles on the PHP Chinese website!