版本5.3
太基础的不讲解,讲一些见解
快速生成auth
php artisan migrate
php artisan make:auth
生成文件
image.png
路由配置
routes/web.php
Auth::routes()
也可以写成
App::make('router')->auth();
上面的路由,其实对应如下路由
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
看下注册
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
对应控制器
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
看到这个
use RegistersUsers;
说明用了trait定义类,实现快速认证,减少自己写轮子
vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php
注册页面
public function showRegistrationForm()
{
return view('auth.register');
}
如果你自己需要更改页面,则在控制器重写这个方法即可
注册提交页面
public function register(Request $request)
{
//验证规则
$this->validator($request->all())->validate();
//启动事件,并且注册调用create的create方法来写入数据
event(new Registered($user = $this->create($request->all())));
//使用哪个guard(认证器)这个就是决定你用什么表,一会说
$this->guard()->login($user);
//判断registered是否写了返回,如果有则使用,如果没有,则调用redirectPath这个方法,这个方法里面还判断了很多,如果你的控制器写了protected $redirectTo = '/home';这个属性,则使用这个来实现注册成功跳转
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
好了这个注册认证就那么简单,这个时候,比如我现在有个需求
我分3端来认证怎么办,3端分表为:
用户注册,商家注册,管理员注册
这个时候就用了多认证,首先来配置下config/auth.php
<?php
return [
/*默认用户认证既是Auth::guard()*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
//商家的认证驱动
'providers' => [
'driver' => 'session',
'provider' => 'providers',
],
//后台管理员认证驱动
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
//对应上面的provider
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
//假设商家的模型叫Provider
'providers' => [
'driver' => 'eloquent',
'model' => App\Provider::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
配置好上面,假设我现在要增加一个商家注册呢
创建迁移表,
php artisan make:migration create_table_providers --create=providers
把用户的表结构复制到providers迁移表里面这里,为了方便,就复制一样的了
php artisan migrate
创建类
php artisan make:model Provider
Provider.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Provider extends Authenticatable
{
use Notifiable;
//
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
创建中间件
php artisan make:middleware ProviderMiddleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class ProviderMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*$guard='providers'定义这个为默认驱动
*/
public function handle($request, Closure $next,$guard='providers')
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('provider/auth/regist');
}
}
return $next($request);
}
}
首先是注册路由
Route::group(['prefix'=>'provider'],function(){
Route::group(['prefix'=>'auth','namespace'=>'ProvidersAuth'],function(){
Route::get('/regist','RegistController@showRegistrationForm');
Route::post('/regist','RegistController@register');
});
Route::get('/', ['middleware'=>'providers','uses'=>'HomeController@provider']);
});
然后复制自动生成的注册页面内容到目录ProviderAuth下
并重写一些方法
<?php
namespace App\Http\Controllers\ProvidersAuth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Provider;
use Auth;
class RegistController extends Controller
{
//
use RegistersUsers;
protected $redirectTo = '/provider/';
protected $guard='providers';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
public function showRegistrationForm(){
return view('providerauth.register');
}
protected function create(array $data)
{
return Provider::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
//这里是关键拉,表示我使用providers,如果不重写,则默认使用了user
protected function guard()
{
return Auth::guard($this->guard);
}
}
复制auth下视图到providerauth,然后更改里面的路径地址,即可
这个时候浏览器打开
http://192.168.132.200:82/provider/
因为没有登录,经过中间件之后,调转到regist
image.png
注册一个看看
image.png
image.png
这个时候就完成了商户的注册了,很简单吧,只需要复制改下其他的,就可以完成了。登录操作也是差不多。
网友评论