【问题标题】:Laravel 5.7 customize resend mail methodLaravel 5.7 自定义重发邮件方法
【发布时间】:2019-03-10 12:57:29
【问题描述】:

我自定义了我的 LoginController 以防止用户在未验证帐户时登录,因为这不是 Laravel 的默认行为。

现在用户正在登录并且帐户未经过验证,我问他是否希望收到新的邮件通知来验证他的帐户。由于我没有 $request->user() 我不知道如何覆盖此方法:

验证控制器.php

public function resend(Request $request)
{
    if ($request->user()->hasVerifiedEmail()) {
        return redirect($this->redirectPath());
    }

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

    return back()->with('resent', true);
}

我考虑过获取用户的邮件,但我如何才能根据他的邮件而不是 Laravel 期望的 user() 向他发送电子邮件?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您有几个选择,其中最简单的方法是使用Signed URLs 允许resend 接受用户id 参数,然后当用户登录并且未通过验证时您重定向他们使用id 访问resend 页面,以在没有活动用户会话的情况下识别他们的帐户。

    例如,您的登录控制器如下所示:

    if (! $user->hasVerifiedEmail()()) {
        return redirect()->to(URL::signedRoute('resend', ['id' => $user->id]));
    }
    

    你的VerificationController@resend 方法看起来像这样:

    public function resend(Request $request)
    {
        if ($request->input('id') && $request->hasValidSignature()) {
            $user = User::findOrFail($request->input('id'));
        }
    
        $user = $user ?: $request->user();
    
        if ($user->hasVerifiedEmail()) {
            return redirect($this->redirectPath());
        }
    
        $user->sendEmailVerificationNotification();
    
        return back()->with('resent', true);
    }
    

    也就是说,Laravel 包含用于要求电子邮件验证的中间件:它确实允许登录,但在用户验证之前不允许用户做任何事情,因此除非您有理由完全阻止登录,否则中间件可以满足您的需求。可以找到中间件here的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2019-03-10
      • 2019-03-05
      • 2023-03-28
      • 1970-01-01
      • 2015-02-14
      • 2020-07-12
      相关资源
      最近更新 更多