如何在电子邮件中为 laravel 上的 api 路由和 web 路由创建不同的密码重置链接

问题描述

所以我正在尝试为路由 API 制作一个忘记密码的功能 当用户访问该功能时,他将收到一封电子邮件,其中包含重置密码的链接。并且邮件中的链接会根据用户使用的路线而有所不同。

示例: 当用户访问API路由时,用户收到的链接是http://website.com/api/password/reset,用户访问web路由时,收到的链接是http://webiste.com/password/reset

我尝试使用 Request :: is ('api *') 并且它在本地服务器上运行流畅,但是当我部署到 heroku 时,Request :: is ('api *') 命令不起作用,因此发送的链接指向我使用的网络路由Request :: is ('api *')ResetPasswordNotification

<?php

namespace Illuminate\Auth\Notifications;

use Illuminate\Http\Request;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;
// use Illuminate\Support\Facades\Request;

class ResetPassword extends Notification
{
    /**
     * The password reset token.
     *
     * @var string
     */
    public $token;

    /**
     * The callback that should be used to create the reset password URL.
     *
     * @var \Closure|null
     */
    public static $createUrlCallback;

    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

    /**
     * Create a notification instance.
     *
     * @param  string  $token
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback,$notifiable,$this->token);
        }

        if (static::$createUrlCallback) {
            $url = call_user_func(static::$createUrlCallback,$this->token);
        } else {
            $request = new Request;
            if($request->is('api*')){
                $url = url(route('api.password.reset',[
                    'token' => $this->token,'email' => $notifiable->getEmailForPasswordReset(),],false));
            }else{
                $url = url(route('password.reset',false));
            }
        }

        return (new MailMessage)
            ->subject(Lang::get('Reset Password Notification'))
            ->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
            ->action(Lang::get('Reset Password'),$url)
            ->line(Lang::get('This password reset link will expire in :count minutes.',['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
            ->line(Lang::get('If you did not request a password reset,no further action is required.'));
    }

    /**
     * Set a callback that should be used when creating the reset password button URL.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function createUrlUsing($callback)
    {
        static::$createUrlCallback = $callback;
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}

上面的命令在本地服务器上运行流畅,没有任何错误,但在heroku上不起作用。

我请求你帮助解决这个问题。 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)