Introduction
Laravel 5.3 of Auth certification on the basis of 5.2 and some changes, this article explains how to do different user tables in Laravel 5.3 login authentication. Brief introduction of Auth Authentication principle
Laravel certification is the use of guard and provider with the completion of the guard responsible for the authentication of the business logic, authentication information service-side preservation, etc. provider is responsible for providing certification information to provide persistent data.
The request is submitted to the guard, and the guard extracts the data (like user name, password, etc.) from the provider to verify that the input data matches the data stored on the server side. If the submitted data is correct, then do the session and other business processing (if necessary). Certified Scaffolding
First we import Laravel's own certified scaffolding
PHP Artisan Make:auth
To perform a database migration:
PHP Artisan Migrate
Modify the Auth certified configuration file config/auth.php
At Gurads, add admin guard for background admin authentication
' Guards ' + [
' web ' = [
' driver ' = ' session ',
' provider ' + ' users ',
],
' admin ' [
' Driver ' = ' session ',
' provider ' = ' admins ',
],
' API ' = [
' driver ' = ' token ', c11/> ' provider ' and ' users ',
],
Add admins provider at providers, using the Admin model
' Providers ' = [
' users ' and ' = '
driver ' = ' eloquent ',
' model ' + app\user::class,
],< c19/> ' admins ' = [
' driver ' + ' eloquent ',
' model ' = App\admin::class,
],
],
Create a background administrator model
We then create an admin model for background admin login verification.
PHP Artisan Make:model admin-m
The-m parameter generates a database migration file at the same time xxxx_create_admins_table
Modifying the app/admin.php model file
<?php
namespace App;
Use illuminate\notifications\notifiable;
Use Illuminate\foundation\auth\user as authenticatable;
Class Admin extends authenticatable
{use
notifiable;
/**
* The attributes that is mass assignable.
*
* @var array */
protected $fillable = [
' name ', ' Password ',
];
/**
* The attributes that should is hidden for arrays.
*
* @var array */
protected $hidden = [
' password ', ' Remember_token ',
];
}
Edit the Xxxx_create_admins_table file, the background administrator model structure is similar to the foreground user, remove the email field, the Name field is set to unique
<?php use
Illuminate\support\facades\schema;
Use Illuminate\database\schema\blueprint;
Use illuminate\database\migrations\migration;
Class Createadminstable extends migration
{
/**
* Run the migrations.
*
* @return void */public
function up ()
{
schema::create (' admins ', function (Blueprint $table {
$table->increments (' id ');
$table->string (' name ')->unique ();
$table->string (' password ');
$table->remembertoken ();
$table->timestamps ();
});
}
/**
* Reverse the migrations.
*
* @return void
*
/Public Function down ()
{
Schema::d ropifexists (' admins ');}
}
The administrator model populates the data
Define a data model factory and add the following code to the database/factories/modelfactory.php
$factory->define (App\admin::class, function (Faker\generator $faker) {
static $password;
return [
' name ' = = $faker->firstname,
' password ' and $password?: $password = Bcrypt (' secret '),
' Remember_token ' = Str_random (Ten),
];
});
Randomly populate user names with Faker
Generate the adminstableseeder.php file in the Database/seeds directory.
PHP Artisan Make:seeder Adminstableseeder
Edit the Run method of the database/seeds/adminstableseeder.php file, add 3 admin users, password is 123456
Public function run ()
{
factory (' App\admin ', 3)->create ([
' password ' = = Bcrypt (' 123456 ')
]);
}
Call the Adminstableseeder class in the database/seeds/databaseseeder.php Run method
Public function run ()
{
$this->call (adminstableseeder::class);
}
Perform database Migration commands
PHP Artisan Migrate--seed
A admins table is created in the database and 3 data is generated
ID |
name |
Password |
Remember_token |
Create_at |
Update_at |
1 |
John |
$2y$10$ayd4mow ... |
9p7bycj5wn |
2016-09-12 11:12:37 |
2016-09-12 11:12:37 |
2 |
Ransom |
$2y$10$ayd4mow ... |
Ct8w5nmtsg |
2016-09-12 11:12:37 |
2016-09-12 11:12:37 |
3 |
Dulce |
$2y$10$ayd4mow ... |
I8rjpxwvrk |
2016-09-12 11:12:37 |
2016-09-12 11:12:37 |
Create a background page
Create a controller
PHP artisan make:controller admin/logincontroller
php artisan make:controller admin/indexcontroller
Among them, Admin/logincontroller is responsible for the login logic; Admin/indexcontroller manage the first page after login.
Edit admin/logincontroller.php
<?php namespace App\http\controllers\admin;
Use App\http\controllers\controller;
Use Illuminate\foundation\auth\authenticatesusers; Class Logincontroller extends Controller {/* |------------------------------------------------------------------- -------
|
Login Controller |--------------------------------------------------------------------------| | This controller handles authenticating users for the application and | Redirecting them to your home screen. The controller uses a trait |
To conveniently provide it functionality to your applications.
|
*/Use Authenticatesusers;
/** * Where to redirect users after Login/registration.
* * @var String */protected $redirectTo = '/admin ';
/** * Create a new controller instance. * * @return void */Public function __construct () {$this->middleware (' guest.admin ', [' except
' = = ' logout ']); }/** * Show background loginRecord template */Public function Showloginform () {return view (' Admin.login ');
}/** * Use admin guard */protected function Guard () {return auth ()->guard (' admin ');
}/** * Rewrite validation using the Username field */Public function username () {return ' name '; }
}
Edit admin/indexcontroller.php
<?php
namespace App\http\controllers\admin;
Use Illuminate\http\request;
Use app\http\requests;
Use App\http\controllers\controller;
Class Indexcontroller extends Controller
{
/**
* Displays the Admin Template home page *
/Public Function index ()
{
return view (' Admin.index ');
}
}
Background Display Templates
Copy views/layouts/app.blade.php into views/layouts/admin.blade.php
Edit a background Management layout template
<! DOCTYPE html>
Copy views/auth/login.blade.php into views/admin/login.blade.php
Edit the template, change the layout file to layouts.admin, replace the submission URL of the form with the Admin/login,email field, and remove the part that retrieves the password.
@extends (' layouts.admin ')
@section (' content ')
<div class= "container" >
<div class= "Row" >
<div class= "col-md-8 col-md-offset-2" >
<div class= "Panel Panel-default" >
<div class= " Panel-heading ">admin login</div>
<div class=" Panel-body ">
<form class=" Form-horizontal " role= "Form" method= "POST" action= "{{URL ('/admin/login ')}}" >
{{Csrf_field ()}}
<div class= " form-group{{$errors->has (' name ')? ' Has-error ': '} ' >
<label for= ' name ' Clas