1

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

例如:当用户访问API路由时,用户会收到链接http://website.com/api/password/reset,当用户访问web路由时会收到链接http://webiste .com/密码/重置

我试过使用Request :: is ('api *')它,它在本地服务器上运行顺利,但是当我部署到 heroku 时,Request :: is ('api *')命令不起作用,所以发送的链接指向我Request :: is ('api *')在里面使用的 web 路由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, $notifiable, $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', [
                    'token' => $this->token,
                    'email' => $notifiable->getEmailForPasswordReset(),
                ], 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 上不起作用。

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

4

1 回答 1

0

您应该使用辅助方法request()从 Laravel 应用程序的任何位置获取当前请求。 https://laravel.com/docs/8.x/helpers#method-request

于 2021-09-21T12:20:15.667 回答