前言
本文主要介绍了关于Laravel 5.5基于内置的Auth模块实现前后台登陆的相关内容,更多关于Auth模块的内容大家可以参考这篇文章:http://www.zzvips.com/article/171443.html
下面话不多说了,来一起看看详细的介绍吧。
方法如下:
步骤1:生成Auth
在项目建立完成后,生成内置Auth组件:
1
|
php artisan make:auth
|
步骤2:添加guard
打开config\auth.php,为方便前后台用户切换,项目共用Users表
1
2
3
4
5
6
7
8
|
'guards' => [
//...
'admin' => [
'driver' => 'session' ,
'provider' => 'users' ,
],
//...
],
|
步骤3:实现基类
1
2
3
4
5
6
7
8
9
|
class AdminController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
$this ->middleware( 'auth:admin' );
}
}
|
步骤4:实现后台登陆控制器
新建appcontrollersAdminLoginController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<?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' );
}
}
|
步骤5:实现登陆后跳转到不同路径
app\Middleware\RedirectIfAuthenticated.php
1
2
3
4
5
6
7
8
|
public function handle( $request , Closure $next , $guard = null)
{
if (Auth::guard( $guard )->check()) {
$path = $guard ? '/admin' : '/home' ;
return redirect( $path );
}
return $next ( $request );
}
|
步骤6:实现未通过认证跳转不同登陆页
app\Exceptions\Handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* 重写实现未认证用户跳转至相应登陆页
* @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' );
}
}
|
完成
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:https://segmentfault.com/a/1190000012509079