I have been using laravel5.5 recently and implemented a function. I will share it with you below. This article mainly introduces you to the relevant information on how Laravel 5.5 implements front-end and back-end login based on the built-in Auth module. The article uses examples The code is introduced in very detail. Friends who need it can refer to it. Let's learn with the editor.
Preface
I won’t say much more, let’s take a look at the detailed introduction.
The method is as follows:
Step 1: Generate Auth
After the project is established, Generate built-in Auth component:
php artisan make:auth
Step 2: Add guard
Open config\auth.php for convenience Front and back user switching, 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 the background login controller
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); }
Step 6: Jump 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'); } }
Complete
Summary
PHP Two-dimensional array implementation method to remove duplicates
Summary of Thinkphp5 behavior usage methods
ajax php implementation without refresh Examples of verifying mobile phone numbers
The above is the detailed content of Detailed explanation of front-end and back-end login in Laravel 5.5 based on the built-in Auth module. For more information, please follow other related articles on the PHP Chinese website!