Laravel 5.2 中多用戶認證明現(前臺和後臺登陸)

Laravel 5.2中多用戶認證支持,即同時容許不一樣數據表用戶(如前臺用戶、後臺用戶、app用戶等)登陸認證。下面咱們就來簡單介紹多用戶登陸及註冊功能實現。php

一、生成認證腳手架

首先咱們使用Laravel 5.2提供的認證腳手架完成初始化工做,打開終端輸入:css

php artisan make:auth

 該Artisan命令會生成用戶認證所需的路由、視圖以及HomeController:html

 

接着去查看路由文件routes.php,會發現該文件已經被更新:jquery

 

 

其中 Route::auth() 定義了註冊登陸路由, /home 爲認證經過後跳轉路由。web

這裏我重寫了一下登陸驗證的方法:ajax

Route::group(['middleware' => 'web'], function () { //Route::auth(); //前臺用戶
    Route::any('home/login', 'Auth\AuthController@login'); Route::get('home/logout', 'Auth\AuthController@logout'); Route::any('home/register', 'Auth\AuthController@register'); Route::get('/home', 'HomeController@index'); });

這裏的Auth文件夾和類文件在 app/Http/Controllers/Auth 目錄下:數據庫

 

 接着咱們打開AuthContrller.php文件,修改或添加本身的類方法,表的字段名按照數據庫建立好的來填寫,下面是我修改過的,你們也能夠參考:bootstrap

 

<?php namespace App\Http\Controllers\Auth; use App\User; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Validator; use App\Http\Requests; use Auth; use Redirect; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; class AuthController extends Controller { /* |-------------------------------------------------------------------------- | Registration & Login Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users, as well as the | authentication of existing users. By default, this controller uses | a simple trait to add these behaviors. Why don't you explore it? | */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; /** * Where to redirect users after login / registration. * * @var string */
    protected $redirectTo = '/'; /** * Create a new authentication controller instance. * * @return void */
    public function __construct() { $this->middleware('guest', ['except' => 'logout']); } //登陸頁面
    public function login(Request $request) { if ($request->isMethod('post')) { $validator = $this->validateLogin($request->input()); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } if (Auth::guard('web')->attempt($this->validateUser($request->input()))) { $request->session()->put('email', $request->email); return Redirect::to('home')->with('success', '登陸成功!'); }else { return back()->with('error', '帳號或密碼錯誤')->withInput(); } } return view('auth.login'); } //登陸頁面驗證
    protected function validateLogin(array $data) { return Validator::make($data, [ 'email' => 'required',
            'password' => 'required', ], [ 'required' => ':attribute 爲必填項',
            'min' => ':attribute 長度不符合要求' ], [ 'email' => '郵箱',
            'password' => '密碼' ]); } //驗證用戶字段
    protected function validateUser(array $data) { return [ 'email' => $data['email'],
            'password' => $data['password'] ]; } //退出登陸
    public function logout() { if(Auth::guard('web')->user()){ Auth::guard('web')->logout(); } return Redirect::to('home'); } //註冊
    public function register(Request $request) { if ($request->isMethod('post')) { $validator = $this->validateRegister($request->input()); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } $user = new User(); $user->name = $request->name;$user->email = $request->email; $user->password = bcrypt($request->password); $user->create_time = time(); $user->update_time = time(); if($user->save()){ return redirect('home/login')->with('success', '註冊成功!'); }else{ return back()->with('error', '註冊失敗!')->withInput(); } } return view('auth.register'); } protected function validateRegister(array $data) { return Validator::make($data, [ 'name' => 'required|alpha_num|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'password_confirmation' => 'required|min:6|' ], [ 'required' => ':attribute 爲必填項',
            'min' => ':attribute 長度不符合要求',
            'confirmed' => '兩次輸入的密碼不一致',
            'unique' => '該郵箱已經被人佔用',
            'alpha_num' => ':attribute 必須爲字母或數字' ], [ 'name' => '暱稱',
            'email' => '郵箱',
            'password' => '密碼',
            'password_confirmation' => '確認密碼' ]); } }

而後修改app目錄下的 User.php:api

<?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { /** * The attributes that are mass assignable. * * @var array */
    protected $table = 'users'; //指定主鍵
    protected $primaryKey = 'userid'; //指定容許批量賦值的字段
    protected $fillable = [ 'name', 'email', 'password', ]; //指定不容許批量賦值的字段
     protected $guarded = []; //自動維護時間戳
    public $timestamps = false; //定製時間戳格式
    protected $dateFormat = 'U'; /** * 避免轉換時間戳爲時間字符串 */
    public function fromDateTime($value) { return $value; } //將默認增長時間轉化爲時間戳
    protected function getDateFormat() { return time(); } /** * The attributes excluded from the model's JSON form. * * @var array */
    protected $hidden = [ 'password', 'remember_token', 'app_token','email' ]; }

 

再去 resource/view/auth 目錄下把login.balde.php 和register.blade.php 的form表單 action 分別改成  action="{{ url('home/login') }}" 和 action="{{ url('home/register') }}", 和layouts/app.blade.php 改成:瀏覽器

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel='stylesheet' type='text/css'>
    <link href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700" rel='stylesheet' type='text/css'>

    <!-- Styles -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> {{-- <link href="{{ elixir('css/app.css') }}" rel="stylesheet"> --}} <style> body { font-family: 'Lato';
        } .fa-btn { margin-right: 6px;
        }
    </style>
</head>
<body id="app-layout">
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">

                <!-- Collapsed Hamburger -->
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                    <span class="sr-only">Toggle Navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <!-- Branding Image -->
                <a class="navbar-brand" href="{{ url('/') }}"> Laravel </a>
            </div>

            <div class="collapse navbar-collapse" id="app-navbar-collapse">
                <!-- Left Side Of Navbar -->
                <ul class="nav navbar-nav">
                    <li><a href="{{ url('/home') }}">Home</a></li>
                </ul>

                <!-- Right Side Of Navbar -->
                <ul class="nav navbar-nav navbar-right">
                    <!-- Authentication Links --> @if (Auth::guest()) <li><a href="{{ url('home/login') }}">Login</a></li>
                        <li><a href="{{ url('home/register') }}">Register</a></li> @else <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> {{ Auth::user()->name }} <span class="caret"></span>
                            </a>

                            <ul class="dropdown-menu" role="menu">
                                <li><a href="{{ url('home/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
                            </ul>
                        </li> @endif </ul>
            </div>
        </div>
    </nav> @yield('content') <!-- JavaScripts -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> {{-- <script src="{{ elixir('js/app.js') }}"></script> --}} </body>
</html>

 

還有修改 app/Http/Middleware/Authenticate.php 文件:

<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class Authenticate { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */
    public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('home/login'); } } return $next($request); } }

 

二、實現前臺用戶登陸

接下來咱們先實現前臺用戶登陸,也就是Laravel自帶的User用戶登陸。經過上面的腳手架,咱們已經生成了認證所需的全部代碼,剩下要作的就是使用遷移命令建立用戶認證相關表:

 

php artisan migrate

 

該命令執行後生成 users 表和 password_resets 表,分別爲用戶主表和密碼重置表。

而後咱們就能夠在瀏覽器中輸入 http://localhost/home 訪問頁面,發現頁面自動跳轉到了登陸頁面,這裏的原理就是 app/Http/Middleware/Authenticate.php 文件,如今咱們是登陸不上去了,因此咱們先點擊右上角的register註冊一個用戶:

 

 

成功登錄後就能夠看到效果了:

這是前臺用戶登陸的效果,而後看後臺登陸的實現。

三、編輯認證配置文件

要實現多用戶認證,首先要配置認證配置文件,在 config 目錄下的auth.php,配置以下:

<?php return [ /* |-------------------------------------------------------------------------- | Authentication Defaults |-------------------------------------------------------------------------- | | This option controls the default authentication "guard" and password | reset options for your application. You may change these defaults | as required, but they're a perfect start for most applications. | */

    'defaults' => [ 'guard' => 'web',
        'passwords' => 'users', ],

    /* |-------------------------------------------------------------------------- | Authentication Guards |-------------------------------------------------------------------------- | | Next, you may define every authentication guard for your application. | Of course, a great default configuration has been defined for you | here which uses session storage and the Eloquent user provider. | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | Supported: "session", "token" | */

    'guards' => [ 'web' => [ 'driver' => 'session',
            'provider' => 'users', ],

        'api' => [ 'driver' => 'token',
            'provider' => 'users', ],
        'admin' => [ 'driver' => 'session',
            'provider' => 'admins', ], ],

    /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */

    'providers' => [ 'users' => [ 'driver' => 'eloquent',
            'model' => App\User::class, ],
        'admins' => [ 'driver' => 'eloquent',
            'model' => App\Admin::class, ],

        // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ],
    ],

    /* |-------------------------------------------------------------------------- | Resetting Passwords |-------------------------------------------------------------------------- | | Here you may set the options for resetting passwords including the view | that is your password reset e-mail. You may also set the name of the | table that maintains all of the reset tokens for your application. | | You may specify multiple password reset configurations if you have more | than one user table or model in the application and you want to have | separate password reset settings based on the specific user types. | | The expire time is the number of minutes that the reset token should be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | */

    'passwords' => [ 'users' => [ 'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60, ], ], ];

認證是由 guard 和 provider 兩部分構成的(參考用戶認證文檔),因此咱們在這兩個配置項中分別新增了 admin 和 admins 選項。

四、建立後臺用戶模型

接下來咱們來實現後臺用戶登陸,首先使用以下Artisan命令生成後臺用戶模型:

 

php artisan make:model Admin --migration

帶上 --migration 選項會生成對應用戶表 admins ,咱們定義該數據表字段和 users 同樣,生成的文件在database/migration目錄下:

 

 

Schema::create('admins', function (Blueprint $table) { $table->increments('adminid'); $table->string('name'); $table->string('account_number', 128); $table->string('password', 64); $table->rememberToken(); $table->integer('create_time'); $table->integer('update_time'); });

 

而後經過運行遷移命令生成該表:

 

php artisan migrate

 

而後更新 Admin 模型類以下:

 

<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Auth\User as Authenticatable; class Admin extends Authenticatable { //表名
    protected $table = 'admins'; //指定主鍵
    protected $primaryKey = 'adminid'; //指定容許批量賦值的字段
    protected $fillable = [ 'name', 'account_number', 'password', ]; //指定不容許批量賦值的字段 // protected $guarded = []; //自動維護時間戳
    public $timestamps = false; //定製時間戳格式
    protected $dateFormat = 'U'; //將默認增長時間轉化爲時間戳
    protected function getDateFormat() { return time(); } /** * The attributes excluded from the model's JSON form. * * @var array */
    protected $hidden = [ 'password', 'remember_token' ]; }

 

五、定義後臺用戶認證路由及控制器

接下來咱們來定義後臺用戶認證路由,修改 routes.php 代碼以下:

 

Route::group(['middleware' => 'web'], function () { //Route::auth(); //前臺用戶
    Route::any('home/login', 'Auth\AuthController@login'); Route::get('home/logout', 'Auth\AuthController@logout'); Route::any('home/register', 'Auth\AuthController@register'); Route::get('/home', 'HomeController@index'); //後臺管理員
    Route::any('admin/login', 'Admin\AuthController@login'); Route::any('admin/logout', 'Admin\AuthController@logout'); Route::any('admin/register', 'Admin\AuthController@register'); Route::get('/admin', 'AdminController@index'); });

而後使用Artisan命令建立對應控制器:

php artisan make:controller Admin/AuthController php artisan make:controller AdminController
php artisan make:middleware AuthAdmin

 

app/Http 目錄下會多出Admin認證的文件夾和AdminController.php文件,修改AdminController.php:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class AdminController extends Controller { /** * Create a new controller instance. * * @return void */
    public function __construct() { $this->middleware('admin'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */
    public function index() { return view('admin.index'); } }

 

編輯 Admin/AuthController.php 代碼以下:

 

<?php namespace App\Http\Controllers\Admin; use App\Admin; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Validator; use App\Http\Requests; use Auth; use Redirect; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; class AuthController extends Controller { use AuthenticatesAndRegistersUsers, ThrottlesLogins; // protected $redirectTo = 'admin'; // protected $guard = 'admin'; // protected $loginView = 'admin.login'; // protected $registerView = 'admin.register';

    public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); } //登陸
    public function login(Request $request) { if ($request->isMethod('post')) { $validator = $this->validateLogin($request->input()); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } if (Auth::guard('admin')->attempt(['account_number'=>$request->account_number, 'password'=>$request->password])) { return Redirect::to('admin')->with('success', '登陸成功!');     //login success, redirect to admin
            } else { return back()->with('error', '帳號或密碼錯誤')->withInput(); } } return view('admin.login'); } //登陸頁面驗證
    protected function validateLogin(array $data) { return Validator::make($data, [ 'account_number' => 'required|alpha_num',
            'password' => 'required', ], [ 'required' => ':attribute 爲必填項',
            'min' => ':attribute 長度不符合要求' ], [ 'account_number' => '帳號',
            'password' => '密碼' ]); } //退出登陸
    public function logout() { if(Auth::guard('admin')->user()){ Auth::guard('admin')->logout(); } return Redirect::to('admin/login'); } //註冊
    public function register(Request $request) { if ($request->isMethod('post')) { $validator = $this->validateRegister($request->input()); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } $user = new Admin(); $user->name = $request->name; $user->account_number = $request->account_number; $user->password = bcrypt($request->password); $user->create_time = time(); $user->update_time = time(); if($user->save()){ return redirect('admin/login')->with('success', '註冊成功!'); }else{ return back()->with('error', '註冊失敗!')->withInput(); } } return view('admin.register'); } protected function validateRegister(array $data) { return Validator::make($data, [ 'name' => 'required|alpha_num|max:255',
            'account_number' => 'required|alpha_num|unique:admins',
            'password' => 'required|min:6|confirmed',
            'password_confirmation' => 'required|min:6|' ], [ 'required' => ':attribute 爲必填項',
            'min' => ':attribute 長度不符合要求',
            'confirmed' => '兩次輸入的密碼不一致',
            'unique' => '該帳戶已存在',
            'alpha_num' => ':attribute 必須爲字母或數字',
            'max' => ':attribute 長度過長' ], [ 'name' => '暱稱',
            'account_number' => '帳號',
            'password' => '密碼',
            'password_confirmation' => '確認密碼' ]); } }

 

編輯 Middleware/AuthAdmin.php 代碼以下:

 

<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class AuthAdmin { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */
    public function handle($request, Closure $next, $guard = null) { if (Auth::guard('admin')->guest()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('admin/login'); } } return $next($request); } }

 

最後再修改Http/Kernel.php,最下方添加一個 admin 認證用戶指向:

 

protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'admin' => \App\Http\Middleware\AuthAdmin::class, ];

 

六、視圖文件建立及修改

最後咱們要建立後臺用戶認證對應視圖文件,這裏咱們簡單拷貝前臺用戶視圖模板並稍做修改便可:

 

cp -r resources/views/auth resources/views/admin

 

修改 resources/views/admin 目錄下登陸及註冊表單提交地址:

login.blade.php以下:

 

 

@extends('layouts.admin')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Login</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('admin/login') }}">
                        {!! csrf_field() !!}

                        <div class="form-group{{ $errors->has('account_number') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Account Number</label>

                            <div class="col-md-6">
                                <input type="text" class="form-control" name="account_number" value="{{ old('account_number') }}">

                                @if ($errors->has('account_number'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('account_number') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember"> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-sign-in"></i>Login
                                </button>

                                <a class="btn btn-link" href="{{ url('/password/reset') }}">Forgot Your Password?</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

 

register.blade.php 以下:

 

 

@extends('layouts.admin')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Register</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('admin/register') }}">
                        {!! csrf_field() !!}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Name</label>

                            <div class="col-md-6">
                                <input type="text" class="form-control" name="name" value="{{ old('name') }}">

                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('account_number') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Acoount Number</label>

                            <div class="col-md-6">
                                <input type="text" class="form-control" name="account_number" value="{{ old('account_number') }}">

                                @if ($errors->has('account_number'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('account_number') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Confirm Password</label>

                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password_confirmation">

                                @if ($errors->has('password_confirmation'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password_confirmation') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-user"></i>Register
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

 

再把 home.blade.php 複製到admin下,重名爲index.php,app.blade.php 複製一個並重命名爲admin.ablde.php:

 

cp /resources/views/home.blade.php /resources/views/index.blade.php
cp /resources/views/layouts/app.blade.php /resources/views/layouts/admin.blade.php

 

admin.blade.php 修改以下:

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel='stylesheet' type='text/css'>
    <link href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700" rel='stylesheet' type='text/css'>

    <!-- Styles -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    {{-- <link href="{{ elixir('css/app.css') }}" rel="stylesheet"> --}}

    <style>
        body {
            font-family: 'Lato';
        }

        .fa-btn {
            margin-right: 6px;
        }
    </style>
</head>
<body id="app-layout">
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">

                <!-- Collapsed Hamburger -->
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                    <span class="sr-only">Toggle Navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <!-- Branding Image -->
                <a class="navbar-brand" href="{{ url('/') }}">
                    Laravel
                </a>
            </div>

            <div class="collapse navbar-collapse" id="app-navbar-collapse">
                <!-- Left Side Of Navbar -->
                <ul class="nav navbar-nav">
                    <li><a href="{{ url('/admin') }}">Home</a></li>
                </ul>

                <!-- Right Side Of Navbar -->
                <ul class="nav navbar-nav navbar-right">
                    <!-- Authentication Links -->
                    @if (!Auth::guest('admin')->user())
                        <li><a href="{{ url('admin/login') }}">Login</a></li>
                        <li><a href="{{ url('admin/register') }}">Register</a></li>
                    @else
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                {{ Auth::guard('admin')->user()->name }} <span class="caret"></span>
                            </a>

                            <ul class="dropdown-menu" role="menu">
                                <li><a href="{{ url('admin/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
                            </ul>
                        </li>
                    @endif
                </ul>
            </div>
        </div>
    </nav>

    @yield('content')

    <!-- JavaScripts -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    {{-- <script src="{{ elixir('js/app.js') }}"></script> --}}
</body>
</html>

 

 

七、實現後臺用戶認證

在瀏覽器中訪問 http://localhost/admin/register ,一樣顯示註冊頁面:

 

 

 

 註冊並登錄後顯示:

好了,至此咱們已經完成先後臺用戶同時登陸認證功能。

相關文章
相關標籤/搜索