【问题标题】:Multi authentication for email verification | Laravel电子邮件验证的多重身份验证 |拉拉维尔
【发布时间】:2020-07-19 10:21:13
【问题描述】:

在laravel的注册系统中有一个函数guard()

protected function guard(){
   return Auth::guard();
}

在位置

vendor/laravel/ui/auth-backend/RegisterUsers.php

而寄存器的功能就是使用这个守卫功能

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

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

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

    if ($response = $this->registered($request, $user)) {
        return $response;
    }

    return $request->wantsJson()
                ? new Response('', 201)
                : redirect($this->redirectPath());
}

当我想为另一个守卫做一个注册系统时,我只会覆盖功能守卫功能并像这样保持它

protected function guard(){
   return Auth::guard('admin');
}

所有这些都非常好 但是在邮件验证中,guard() 函数不存在 它使用 $request->user() 而是这样

public function resend(Request $request)
{
    if ($request->user()->hasVerifiedEmail()) {
        return $request->wantsJson()
                    ? new Response('', 204)
                    : redirect($this->redirectPath());
    }

    $request->user()->sendEmailVerificationNotification();

    return $request->wantsJson()
                ? new Response('', 202)
                : back()->with('resent', true);
}

那么,如果我想在这种情况下覆盖守卫怎么办 我想覆盖(重新发送)的整个功能,但我认为这不是最好的解决方案 谁能告诉我该怎么做

【问题讨论】:

    标签: php laravel authentication


    【解决方案1】:

    而不是覆盖守卫的功能 我只会用 shouldUse() 构造函数中的函数 像那样 auth()->shouldUse('admin'); 然后当我使用request->user() 时,它会自动将其用作管理员警卫

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多