自定义 Laravel 5.7


原理解析:

验证邮箱在Laravel默认实现中是一个Notification,不是Mailable,而为了自定义验证邮箱的默认配置,我们先来查看一下

/vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php 文件:

  1 /**
  2      * Build the mail representation of the notification.
  3      *
  4      * @param  mixed  $notifiable
  5      * @return IlluminateNotificationsMessagesMailMessage
  6      */
  7     public function toMail($notifiable)
  8     {
  9         $verificationUrl = $this->verificationUrl($notifiable);
 10 
 11         if (static::$toMailCallback) {
 12             return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
 13         }
 14 
 15         return (new MailMessage)
 16             ->subject(Lang::get('Verify Email Address'))
 17             ->line(Lang::get('Please click the button below to verify your email address.'))
 18             ->action(Lang::get('Verify Email Address'), $verificationUrl)
 19             ->line(Lang::get('If you did not create an account, no further action is required.'));
 20     }

实现修改:

可以看到,默认验证邮件需要的subject就在这个方法内被配置好了,Lang::get()方法会根据laravel app程序设置的语言,自动翻译参数中的文本。

在 resources/lang 文件夹下创建一个en.json文件,添加原始文本及其替换文本即可实现subject等默认文本的自定义,示例如下:

  1 {
  2     "Verify Email Address": "My preferred subject",
  3     "Please click the button below to verify your email address.":"Another translation"
  4 }

如果需要翻译文本为其他语言,可以安装语言包或者自己创建一个对应的翻译文本,并在 config/app.php 文件中修改 locale值即可。

原文地址:https://www.cnblogs.com/dzkjz/p/12368865.html