线上环境重置密码的邮件中的链接变成了 localhost
我做到这里, 本地环境测试的结果都是正常的. 但是线上环境重置密码的邮件中的链接变成了 localhost, 如图:
我上一遍做的是 Laravel 5.5 的版本. 我又打开了上一遍做的线上版本, 测试结果是正常的. 如下图:
因为这一节的内容是发送邮件, 这个功能的逻辑是在框架中写好的, 我并没有做多余的修改.
我看了一些讨论, 邮件中的链接内容应该与 toMail 方法有关, 这是 toMail 方法的内容:
/**
* 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);
}
return (new MailMessage)
->subject(Lang::getFromJson('Reset Password Notification'))
->line(Lang::getFromJson('You are receiving this email because we received a password reset request for your account.'))
->action(Lang::getFromJson('Reset Password'), url(config('app.url').route('password.reset', $this->token, false)))
->line(Lang::getFromJson('If you did not request a password reset, no further action is required.'));
}
其中 ->action(Lang::getFromJson('Reset Password'), url(config('app.url').route('password.reset', $this->token, false))) 中的 config('app.url') 指的是 config/app.php 文件中的 'url' => env('APP_URL', 'http://localhost'),.env 文件中: APP_URL=http://weibo.test
我不清楚具体是哪里的问题, 还是我遗漏了什么.


关于 LearnKu
解决了! 原因是没有添加消息通知, 5.5的教程中是由消息通知的. 消息通知中重写了
toMail方法, 如下:其中
url指定了邮件中的链接, 使用了 路由password.reset的url, 并且route方法的第三个参数false指定了url地址为相对地址, 因此邮件中的链接就是相对于项目地址的了. 即使在线上环境, 链接地址也是正确的.如果没有重写
toMail方法, 则发送邮件会调用\vendor\laravel\framework\src\Illuminate\Auth\Notifications\ResetPassword.php文件中的toMail方法, 就像我在问题中所表述的: 这个方法中的url指定的链接地址使用的是:config('app.url'), 也就是config/app.php文件中的 'url' . 参考.env文件中的内容, 最终指向的都是localhost.这份5.7的文档缺少了
消息通知部分的内容, 还是添上比较好 @Summer