首頁 > 後端開發 > php教程 > laravel5.2實作區分前後台使用者登入的方法

laravel5.2實作區分前後台使用者登入的方法

高洛峰
發布: 2023-03-04 19:16:01
原創
1361 人瀏覽過

1.前台登入

直接使用laravel自帶的auth

php artisan make:auth
登入後複製

然後可以查看路由檔案:

Route::group(['middleware' => 'web'], function () {
 Route::auth();
 Route::get('/home', 'HomeController@index');
});
登入後複製

執行兩張

2.後台登入

編輯設定檔

configauth.php

新增guards中的admin與providers中的admins

<?php
return [
 &#39;defaults&#39; => [
  &#39;guard&#39; => &#39;web&#39;,
  &#39;passwords&#39; => &#39;users&#39;,
 ],
 &#39;guards&#39; => [
  &#39;web&#39; => [
   &#39;driver&#39; => &#39;session&#39;,
   &#39;provider&#39; => &#39;users&#39;,
  ],
  &#39;admin&#39; => [
   &#39;driver&#39; => &#39;session&#39;,
   &#39;provider&#39; => &#39;admins&#39;,
  ],
  &#39;api&#39; => [
   &#39;driver&#39; => &#39;token&#39;,
   &#39;provider&#39; => &#39;users&#39;,
  ],
 ],
 &#39;providers&#39; => [
  &#39;users&#39; => [
   &#39;driver&#39; => &#39;eloquent&#39;,
   &#39;model&#39; => App\User::class,
  ],
  &#39;admins&#39; => [
   &#39;driver&#39; => &#39;eloquent&#39;,
   &#39;model&#39; => App\Admin::class,
  ],
 ],
 &#39;passwords&#39; => [
  &#39;users&#39; => [
   &#39;provider&#39; => &#39;users&#39;,
   &#39;email&#39; => &#39;auth.emails.password&#39;,
   &#39;table&#39; => &#39;password_resets&#39;,
   &#39;expire&#39; => 60,
  ],
 ],
];
登入後複製

 -migration 

然後修改databasemigrations中的資料表結構,複製user表的就可以 

執行php artisan migrate 會發現產生了admin表

4.定義後台路由


Route::group([&#39;prefix&#39; => &#39;admin&#39; ,&#39;middleware&#39; => &#39;admin&#39;], function () {
 Route::get(&#39;login&#39;, &#39;Admin\AuthController@getLogin&#39;);
 Route::post(&#39;login&#39;, &#39;Admin\AuthController@postLogin&#39;);
 Route::get(&#39;register&#39;, &#39;Admin\AuthController@getRegister&#39;);
 Route::post(&#39;register&#39;, &#39;Admin\AuthController@postRegister&#39;);
 Route::get(&#39;logout&#39;, &#39;Admin\AuthController@logout&#39;);
 Route::get(&#39;/&#39;, &#39;Admin\AdminController@index&#39;);
});
登入後複製


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 = &#39;/admin&#39;;
 protected $guard = &#39;admin&#39;;
 protected $loginView = &#39;admin.login&#39;;
 protected $registerView = &#39;admin.register&#39;;
 public function __construct()
 {
  $this->middleware(&#39;guest:admin&#39;, [&#39;except&#39; => &#39;logout&#39;]);
 }
 protected function validator(array $data)
 {
  return Validator::make($data, [
   &#39;name&#39; => &#39;required|max:255&#39;,
   &#39;email&#39; => &#39;required|email|max:255|unique:admins&#39;,
   &#39;password&#39; => &#39;required|confirmed|min:6&#39;,
  ]);
 }
 protected function create(array $data)
 {
  return Admin::create([
   &#39;name&#39; => $data[&#39;name&#39;],
   &#39;email&#39; => $data[&#39;email&#39;],
   &#39;password&#39; => bcrypt($data[&#39;password&#39;]),
  ]);
 }
}
登入後複製

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(&#39;auth:admin&#39;);
 }
 public function index()
 {
  $admin = Auth::guard(&#39;admin&#39;)->user();
  return view(&#39;admin.home&#39;);
 }
}
登入後複製

現在試試看前後台分別登入吧!


7.可能遇到的頁面跳躍問題

如果你遇到了這種情況可以嘗試修改一下MiddlewareAuthenticate.php

<ul class="nav navbar-nav navbar-right">
     <!-- Authentication Links -->
     @if (Auth::guest(&#39;admin&#39;))
      <li><a href="{{ url(&#39;/login&#39;) }}">Login</a></li>
      <li><a href="{{ url(&#39;/register&#39;) }}">Register</a></li>
     @else
      <li class="dropdown">
       <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
        {{ Auth::guard(&#39;admin&#39;)->user()->name }} <span class="caret"></span>
       </a>
       <ul class="dropdown-menu" role="menu">
        <li><a href="{{ url(&#39;/admin/logout&#39;) }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
       </ul>
      </li>
     @endif
    </ul>
登入後複製

以上所述是小編給大家介紹的laravel5.2實現區分前後台用戶登入的方法,希望對大家有幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對PHP中文網的支持!

更多laravel5.2實作區分前後台使用者登入的方法相關文章請關注PHP中文網!


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板