Laravel5.2 implements a method to distinguish between front-end and back-end user logon. laravel5.2 User Logon

Source: Internet
Author: User

Laravel5.2 implements a method to distinguish between front-end and back-end user logon. laravel5.2 User Logon

1. Front-end Login

Directly use the auth provided by laravel

php artisan make:auth

Then you can view the route file:

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

Execute php artisan migrate

Two tables are generated.

2. Background Logon

Edit configuration file

Config \ auth. php

Add admin in guards and admins in providers

<?phpreturn [ '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,  ], ],];

3. Create a data model

Execute php artisan make: model Admin -- migration

Modify the data table structure in database \ migrations and copy the user table.

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

4. Define backend routes

Here I have defined a routing group.

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. Create a controller

Run

php artisan make:controller Admin/AuthControllerphp artisan make:controller Admin/AdminController

For AuthController. php, refer to AuthController. php In Auth.

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

AdminController. php

<?phpnamespace 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'); }}

6. Create a view

Copy the view and home. blade. php In auth to a new admin folder.

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

Modify app. blade. php In layouts

<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>

Now, try logging on to the front and back ends separately!

7. Possible page Jump Problems

If this happens, try to modify Middleware \ Authenticate. php.

return redirect()->guest($guard.'/login');

The above section describes how laravel5.2 distinguishes between frontend and backend user logon. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.