我已經做了多重驗證,但是我最終的代碼有問題。我有這樣的代碼如何在Laravel 5.2中創建多重身份驗證
php artisan make:auth
它會生成用戶表的基本登錄/註冊路由,視圖和控制器。
爲簡單起見,將用戶表製作爲管理員表。
控制器對於管理員 應用程序/ HTTP /控制器/ AdminAuth/AuthController 應用程序/ HTTP /控制器/ AdminAuth/PasswordController :
(注意我剛剛從應用程序/ HTTP /控制器/認證/ AuthController這裏複製這些文件)配置/ auth.php
//Authenticating guards
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
//Resetting Password
'passwords' => [
'clients' => [
'provider' => 'client',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admin',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
route.php
Route::group(['middleware' => ['web']], function() {
//Login Routes...
Route::get('/admin/login','AdminAuth\[email protected]');
Route::post('/admin/login','AdminAuth\[email protected]');
Route::get('/admin/logout','AdminAuth\[email protected]');
// Registration Routes...
Route::get('admin/register', 'AdminAuth\[email protected]');
Route::post('admin/register', 'AdminAuth\[email protected]');
Route::get('/admin', '[email protected]');
});
AdminAuth/AuthController.php
添加兩種方法,並指定$ redirectTo和$後衛
protected $redirectTo = '/admin';
protected $guard = 'admin';
public function showLoginForm()
{
if (view()->exists('auth.authenticate')) {
return view('auth.authenticate');
}
return view('admin.auth.login');
}
public function showRegistrationForm()
{
return view('admin.auth.register');
}
它會幫助你打開另一登錄表單的管理
爲管理員創建一箇中間件
class RedirectIfNotAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = 'admin')
{
if (!Auth::guard($guard)->check()) {
return redirect('/');
}
return $next($request);
}
}
kernel.php中的註冊中間件
protected $routeMiddleware = [
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
use this middleware in AdminController e.g.,
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
public function __construct(){
$this->middleware('admin');
}
public function index(){
return view('admin.dashboard');
}
}
這段代碼是什麼意思Auth::guard('admin')->user()
?我在哪裏輸入該代碼?