1.前台登入
直接使用laravel自帶的auth
php artisan make:auth
然後可以查看路由檔案:
Route::group(['middleware' => 'web'], function () { Route::auth(); Route::get('/home', 'HomeController@index'); });
執行兩張
<?php return [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ], ];-migration
然後修改databasemigrations中的資料表結構,複製user表的就可以
執行php artisan migrate 會發現產生了admin表4.定義後台路由
Route::group(['prefix' => 'admin' ,'middleware' => 'admin'], function () { Route::get('login', 'Admin\AuthController@getLogin'); Route::post('login', 'Admin\AuthController@postLogin'); Route::get('register', 'Admin\AuthController@getRegister'); Route::post('register', 'Admin\AuthController@postRegister'); Route::get('logout', 'Admin\AuthController@logout'); Route::get('/', 'Admin\AdminController@index'); });
5.建立控制器
執行
php artisan make:controller Admin/AuthController php artisan make:controller Admin/AdminController
<?php namespace App\Http\Controllers\Admin; use App\Admin; use Validator; 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']); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:admins', 'password' => 'required|confirmed|min:6', ]); } protected function create(array $data) { return Admin::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } }
6.創建視圖
這裡直接複製auth中的視圖和home.blade.php到一個新的admin資料夾。
修改login和register中表單的action,加上上admin前綴修改layouts中的app.blade.php<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Auth; class AdminController extends Controller { public function __construct() { $this->middleware('auth:admin'); } public function index() { $admin = Auth::guard('admin')->user(); return view('admin.home'); } }現在試試看前後台分別登入吧!
7.可能遇到的頁面跳躍問題
如果你遇到了這種情況可以嘗試修改一下MiddlewareAuthenticate.php
<ul class="nav navbar-nav navbar-right"> <!-- Authentication Links --> @if (Auth::guest('admin')) <li><a href="{{ url('/login') }}">Login</a></li> <li><a href="{{ url('/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>
以上所述是小編給大家介紹的laravel5.2實現區分前後台用戶登入的方法,希望對大家有幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對PHP中文網的支持!
更多laravel5.2實作區分前後台使用者登入的方法相關文章請關注PHP中文網!