首页 >后端开发 >php教程 >Laravel 5.5基于内置的Auth模块实现前后台登陆的详解

Laravel 5.5基于内置的Auth模块实现前后台登陆的详解

jacklove
jacklove原创
2018-07-05 17:38:351361浏览

最近在使用laravel5.5,利用其实现了一个功能,下面分享给大家,这篇文章主要给大家介绍了关于Laravel 5.5基于内置的Auth模块如何实现前后台登陆的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。

前言

下面话不多说了,来一起看看详细的介绍吧。

方法如下:

步骤1:生成Auth

在项目建立完成后,生成内置Auth组件:

php artisan make:auth

步骤2:添加guard

打开config\auth.php,为方便前后台用户切换,项目共用Users表

'guards' => [
 //...
 'admin' => [
  'driver' => 'session',
  'provider' => 'users',
 ],
 //...
 ],

步骤3:实现基类

class AdminController extends BaseController
{
 use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

 public function __construct()
 {
 $this->middleware('auth:admin');
 }
}

步骤4:实现后台登陆控制器

新建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 = &#39;/admin&#39;;
 /**
 * Create a new controller instance.
 *
 * @return void
 */
 public function __construct()
 {
 $this->middleware(&#39;guest:admin&#39;)->except(&#39;logout&#39;);
 }
 /**
 * 重写登陆页面
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
 public function showLoginForm()
 {
 return view(&#39;backend.login&#39;);
 }
 /**
 * 重写退出方法
 * @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(&#39;/admin/login&#39;);
 }
 /**
 * 重写guard认证
 * @return mixed
 */
 protected function guard()
 {
 return Auth::guard(&#39;admin&#39;);
 }
}

步骤5:实现登陆后跳转到不同路径

app\Middleware\RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null)
{
 if (Auth::guard($guard)->check()) {
  $path = $guard ? &#39;/admin&#39; : &#39;/home&#39;;
  return redirect($path);
 }
 return $next($request);
}

步骤6:实现未通过认证跳转不同登陆页

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([&#39;message&#39; => $exception->getMessage()], 401);
 }else{
  return in_array(&#39;admin&#39;, $exception->guards()) ? return redirect()->guest(&#39;/admin/login&#39;) : redirect()->guest(&#39;login&#39;);
 }
 }

完成

总结

您可能感兴趣的文章:

PHP二维数组实现去除重复项的方法

Thinkphp5行为使用方法的汇总

ajax+php实现无刷新验证手机号的实例讲解

以上是Laravel 5.5基于内置的Auth模块实现前后台登陆的详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn