Laravel 5.4 在 2 个不同的 ResetPasswordNotifications 之间切换

Laravel 5.4 Toggle Between 2 Different ResetPasswordNotifications

您将如何在两个不同的 ResetPasswordNotifications 之间切换?

我有通常的重置密码,但是当管理员(在单独的控制器中)创建新用户时,他们也会收到重置密码 "like" 通知。它会有不同的内容,但通过提供 URL 附加生成和存储的令牌来实现相同的目的。

我能看到如何做到这一点的唯一方法是:

  1. 创建我自己的继承 Laravel 的 PasswordBroker
  2. 的 PasswordBroker
  3. 覆盖 PasswordBroker::sendResetLink 以获取额外的参数
  4. 在 PasswordResetServiceProvider 中注册新的 PasswordBroker
  5. 然后当它被调用时,它采用用户名参数(原始)和一个额外的参数来在通知之间切换,例如

    密码::broker()->sendResetLink($username, $myNewToggleParam);

这是执行此操作并维护为用户创建和存储令牌的重置密码功能的最简单方法吗?

找到解决方案,步行到PasswordBroker,然后进入PasswordResetServiceProvider,找出它在服务容器中实例化的位置,然后我就可以做到这一点向管理员刚刚在仪表板中创建的用户发送备用密码重置通知。

为简洁起见,我只放入了少量 public 端点,但为了完整性而添加了它,以防解决方案在没有它的情况下失去上下文:

/**
 * Store a newly created user in storage, and send a notification to
 * indicate the account was created and requires a password reset.
 *
 * @param  \Illuminate\Http\Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function store(Request $request)
{
    $this->authorize('store', User::class);

    $this->validate($request, [...]);

    $user = $this->create($request);

    $this->sendResetLinkEmail($user);

    return response()->json([
        'user' => $user,
        'message' => trans('user.created'),
    ]);
}

/**
 * Send a reset link to the new user.
 *
 * @param  User $user
 */
private function sendResetLinkEmail(User $user)
{
    // Reach into the service container for the password broker
    $passwordBroker = App::make('auth.password.broker');

    // Create a new token from the token repository, and send of the email
    // notification to the new user to reset their password
    $user->sendNewUserPasswordResetNotication(
        $passwordBroker->getRepository()->create($user)
    );
}

不要忘记将新通知添加到用户模型。