laravel调用未定义的方法Auth guard

时间:2018-01-31 07:17:34

标签: laravel laravel-5.5 laravel-authorization

我希望在Laravel 5.5中注册后阻止登录,我已经通过评论一行来做到这一点:

public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

    //    $this->guard()->login($user);

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

我在RegisterController.php

中覆盖它

我收到了这个错误:

Call to undefined method Illuminate\Auth\AuthenticationException::guard()
    $guard = array_get($exception->guard(),0);
    switch ($guard) {
        case 'admin':
            return redirect()->guest(route('admin.login'));
            break;

        default:
            return redirect()->guest(route('login'));
            break;
    }

以下是我的config / auth:

的内容
<?php

return [



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



    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],



    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],


    ],



    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

我启用了多auth系统,我有一个管理员登录和一个用户登录,我不想在我的用户页面注册后禁用登录。

1 个答案:

答案 0 :(得分:0)

在异常中检查后卫可以做这样的事情:

return redirect(route(auth()->guard('admin')->check() ? 'admin.login' : 'login'));

另外,如果您尝试覆盖该方法,请在auth()中使用Auth::帮助器或RegisterController@register外观:

auth()->guard('admin')->login($user);