最近群里很多人文檔,laravel如何做會員和管理兩個身份登錄,今天把教程分享一下
自定義用戶表登錄
認(rèn)證是由 guards 和 providers 兩部分構(gòu)成的, defaults 配置是默認(rèn)選擇一個 guard 認(rèn)證驅(qū)動,所以我們在這兩個配置項中分別添加一個 admin 和 admins 選項。
?php 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], ],
標(biāo)紅的是我們后添加的后臺管理員登錄身份
創(chuàng)建后臺用戶表和model
php artisan make:model Admin php artisan make:migration creaet_admins_table
在數(shù)據(jù)庫遷移文件 _create_admins_table , 我們可以復(fù)制 users 遷移文件里的字段
Schema::create('admins', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email'); $table->string('password'); $table->rememberToken(); $table->timestamps(); });
執(zhí)行php artisan migrate
生成臨時數(shù)據(jù)
在 database/factories/ModelFactory.php, 添加如下數(shù)據(jù):
$factory->define(App\Admin::class, function (Faker\Generator $faker) { static $password; return [ 'name' => $faker->name, 'password' => $password ?: $password = bcrypt('123456'), 'email' => $faker->email, 'remember_token' => str_random(10), ]; });
打開命令行輸入:
php artisan tinker use App; factory(App\Admin::class,5)->create() //生成5條測試數(shù)據(jù),你要幾條就輸入多少(Class,num)
更改 Admin 模型類
?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class Admin extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
創(chuàng)建控制器
php artisan make:controller Admin/LoginController php artisan make:controller Admin/AdminController
編輯 Admin/LoginController.php:
?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\AuthenticatesUsers; class LoginController extends Controller { use AuthenticatesUsers; /** * Where to redirect users after login / registration. * * @var string */ protected $redirectTo = '/admin/index'; protected $username; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest:admin', ['except' => 'logout']); $this->username = config('admin.global.username'); } /** * 重寫登錄視圖頁面 */ public function showLogin() { return view('admin.login.index'); } /** * 自定義認(rèn)證驅(qū)動 * @return mixed */ protected function guard() { return auth()->guard('admin'); } }
修改 app\Http\Middleware\RedirectIfAuthenticated.php:
public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { // 根據(jù)不同 guard 跳轉(zhuǎn)到不同的頁面 $url = $guard ? 'admin/dash':'/home'; return redirect($url); } return $next($request); }
編輯 Admin\AdminController.php:
?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; class AdminController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth.admin:admin'); } // public function index() { dd('用戶名:'.auth('admin')->user()->name); }
編輯 app\Http\Middleware\AdminAuthMiddleware.php
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('admin/login'); } } return $next($request); }
在 app\Http\Kernel.php 中注冊:
protected $routeMiddleware = [ ··· ··· 'auth.admin' => \App\Http\Middleware\AdminAuthMiddleware::class, ];
注冊路由
編輯 routes/web.php :
Route::group(['prefix' => 'admin','namespace' => 'Admin'],function ($router) { $router->get('login', 'LoginController@showLogin')->name('admin.login'); $router->post('login', 'LoginController@login'); $router->post('logout', 'LoginController@logout'); $router->get('index', 'AdminController@index'); });
視圖文件創(chuàng)建和修改
復(fù)制 resources\views\auth\login.blade.php,到 resources\views\admin\login\index.blade.php,修改表單提交地址
{{ url('/login') }} 改成 {{ route('admin.login') }}
訪問 你的站點/admin/login
以上這篇Laravel5.5 實現(xiàn)后臺管理登錄的方法(自定義用戶表登錄)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
標(biāo)簽:迪慶 南寧 烏魯木齊 山南 赤峰 三亞 鷹潭 濟(jì)寧
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel5.5 實現(xiàn)后臺管理登錄的方法(自定義用戶表登錄)》,本文關(guān)鍵詞 Laravel5.5,實現(xiàn),后臺,管理,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。