問題描述
我想在我的 Laravel 項目中進(jìn)行多重身份驗證.
I want to make multiple authentication in my laravel project.
我在 auth.php 文件中創(chuàng)建了新的守衛(wèi)admin",但我不知道如何在我的 authcontroller 中設(shè)置新創(chuàng)建的守衛(wèi).
I create new guard "admin" in my auth.php file but I don't know how to set new created guard in my authcontroller.
它總是使用我的 auth.php 中的默認(rèn)"設(shè)置:
It always use "defaults" settings from my auth.php:
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
推薦答案
1) 如果您希望在整個應(yīng)用程序中使用新創(chuàng)建的 admin
保護(hù),您可以更改 defaults<中的值/strong> 的配置文件.
1) If you wish to use newly created admin
guard throughout the application, you can change the value in defaults of config file.
2) 如果只有 AuthController 使用 Laravel 內(nèi)置的 Auth 系統(tǒng),你可以在 AuthController.php 和 PasswordController.php 中添加這一行 :
2) If it is only about AuthController that uses Laravel's in built Auth system, you can add this line in the AuthController.php and PasswordController.php :
protected $guard = 'admin';
Ref - 檢查 Guard 自定義 此處
3) 如果您希望為任何 Auth 相關(guān)任務(wù)設(shè)置一個非默認(rèn)值的守衛(wèi),您可以像這樣手動指定:
3) If you wish to you a guard other than default one for any Auth related task, you can specify it manually like this :
// For route middleware
Route::get('profile', [
'middleware' => 'auth:admin',
'uses' => 'ProfileController@show'
]);
// For manually logging the user in
if (Auth::guard('admin')->attempt($credentials)) {
// Authenticated...
}
// To login specific user using eloquent model
Auth::guard('admin')->login($user);
// For getting logged in user
Auth::guard('admin')->user();
// To check if user is logged in
if (Auth::guard('admin')->check()) {
// Logged in
}
參考 - https://laravel.com/docs/5.2/authentication
這篇關(guān)于如何在 Laravel 中為 auth 控制器設(shè)置新的保護(hù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!