Rumah  >  Artikel  >  pembangunan bahagian belakang  >  laravel5.2实现区分前后台用户登录的方法

laravel5.2实现区分前后台用户登录的方法

高洛峰
高洛峰asal
2017-01-13 16:54:531332semak imbas

1.前台登录

直接使用laravel自带的auth

php artisan make:auth

然后可以查看路由文件:

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

执行php artisan migrate 

会发现生成了两张表。

2.后台登录

编辑配置文件

config\auth.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,
  ],
 ],
];

   

3.创建数据模型

执行php artisan make:model Admin --migration 

然后修改database\migrations中的数据表结构,复制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

   

AuthController.php可参照Auth中的AuthController.php

<?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;]),
  ]);
 }
}

 

AdminController.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;);
 }
}

6.创建视图

这里直接复制auth中的视图和home.blade.php到一个新的admin文件夹中。 

修改login和register中表单的action,添加上admin前缀

修改layouts中的app.blade.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>

现在试一下前后台分别登录吧!

7.可能遇到的页面跳转问题

如果你遇到了这种情况可以尝试修改一下Middleware\Authenticate.php

return redirect()->guest($guard.&#39;/login&#39;);

以上所述是小编给大家介绍的laravel5.2实现区分前后台用户登录的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHP中文网的支持!

更多laravel5.2实现区分前后台用户登录的方法相关文章请关注PHP中文网!


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn