php - 如何在 Laravel 中为 auth Controller 设置新的保护

标签 php laravel laravel-5

我想在我的 Laravel 项目中进行多重身份验证。

我在 auth.php 文件中创建了新的守卫“admin”,但我不知道如何在我的 authcontroller 中设置新创建的守卫。

它总是使用我的 auth.php 中的“默认”设置:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

最佳答案

1) 如果您想使用新创建的 admin在整个应用程序中保护,您可以更改 中的值默认 的配置文件。

2)如果只是关于 AuthController 使用 Laravel 内置的 Auth 系统,你可以在 AuthController.php 和 PasswordController.php 中添加这一行:

protected $guard = 'admin';

引用 - 检查 guard 定制 here

3)如果您希望为任何与身份验证相关的任务提供除默认值之外的守卫,您可以像这样手动指定它:
// 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

关于php - 如何在 Laravel 中为 auth Controller 设置新的保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38978522/

相关文章:

PHP fatal error : Call to a member function bind_param()

php - 无法再访问 WordPress 管理面板

php - 动态创建单元格时如何设置单元格的填充颜色? TCPDF

php - 将所有请求重定向到 index.html 除了请求到/api 文件夹

php - 如何使用 AJAX 请求显示代码点火器模型中的数据

php - cURL请求已重试3次,但未成功

Laravel 5.4 使用 Mail fake 和 Mail::queue,Mail::assertSent 不起作用

javascript - 如何修复 GET URL 中更改的日期格式

google-chrome - 从 paypal 沙箱返回后,localhost 拒绝连接

PHP-Laravel : Raw Sql Query showing Error in controller