Home > Backend Development > PHP Tutorial > laravel5.2 realizes the method of distinguishing front and back user login

laravel5.2 realizes the method of distinguishing front and back user login

高洛峰
Release: 2023-03-04 19:16:01
Original
1364 people have browsed it

1. Front desk login

Use directly the auth that comes with laravel

php artisan make:auth
Copy after login

Then you can view the routing file:

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

Execute php artisan migrate

You will find that two tables are generated.

2. Background login

Edit configuration file

config\auth.php

Add admin in guards and admins in providers

<?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,
  ],
 ],
];
Copy after login

3. Create a data model

Execute php artisan make:model Admin --migration

Then modify database\migrations For the data table structure in, you can copy the user table

Execute php artisan migrate and you will find that the admin table is generated

4. Define the background route

Here I am Directly defines a routing group

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;);
});
Copy after login

5. Create a controller

Execute

php artisan make:controller Admin/AuthController
php artisan make:controller Admin/AdminController
Copy after login

AuthController.php can refer to 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;]),
  ]);
 }
}
Copy after login


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;);
 }
}
Copy after login

6 in Auth .Create view

Here directly copy the view in auth and home.blade.php to a new admin folder.

Modify the action of the form in login and register, add the admin prefix

Modify app.blade.php in layouts

<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>
Copy after login

Now try to log in to the front and backend separately!

7. Possible page jump problems

If you encounter this situation, you can try to modify Middleware\Authenticate.php

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

The above is the method that the editor introduces to you in laravel5.2 to realize the distinction between front and back user login. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more related articles on laravel5.2's method of distinguishing between front and back user logins, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template