使用Laravel 5.3更改重置密码电子邮件中的视图

时间:2017-02-10 19:29:26

标签: php laravel laravel-5.3 laravel-mail

我正在使用Laravel 5.3,而我现在正在使用我的CRM上的重置密码选项。

我的CRM是多语言的,因此我需要更改电子邮件模板/基于他的语言发送给客户的视图,实际上,我只需要从RTL更改为LTR - 此值设置在cookie上称为“user_direction”。

我正在使用包含ResetPassword类的Laravel默认引导程序身份验证。

这就是现在的情况:

<?php

namespace Illuminate\Auth\Notifications;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
    public $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        $url        = url('password/reset',$this->token);
        $subject    = trans('global.reset_password_email_subject'); 
        $greeting   = trans('global.reset_password_email_greeting'); 
        $line_01    = trans('global.reset_password_email_line_01'); 
        $action     = trans('global.reset_password_email_action'); 
        $line_02    = trans('global.reset_password_email_line_02'); 

        return (new MailMessage)
            ->subject($subject)
            ->greeting($greeting)
            ->line($line_01)
            ->action($action, $url)
            ->line($line_02);
    }
}

这就是我想要的想法,但我不知道如何写得正确:

    public function toMail($notifiable)
    {
        $url        = url('password/reset',$this->token);
        $subject    = trans('global.reset_password_email_subject'); 
        $greeting   = trans('global.reset_password_email_greeting'); 
        $line_01    = trans('global.reset_password_email_line_01'); 
        $action     = trans('global.reset_password_email_action'); 
        $line_02    = trans('global.reset_password_email_line_02'); 

        $view = "notifications::email";
        if($request->cookie('user_direction') == "rtl"):
            $view = "notifications::email-rtl";
        endif;

        return (new MailMessage)
            ->view($view)
            ->subject($subject)
            ->greeting($greeting)
            ->line($line_01)
            ->action($action, $url)
            ->line($line_02);
    }

谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

您可以通过覆盖ResetPasswords Trait中的功能来更改重置密码视图。

只需将以下函数添加到ResetPasswordController并修改您的视图。

/**
 * Display the password reset view for the given token.
 *
 * If no token is present, display the link request form.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  string|null  $token
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function showResetForm(Request $request, $token = null)
{
    return view('auth.passwords.reset')->with(
        ['token' => $token, 'email' => $request->email]
    );
}

答案 1 :(得分:0)

在youtube上观看一些教程后,它对我有用,我这样写道:

首先我添加&#34;使用Cookie;&#34;

public function toMail($notifiable)
{
    $user_language_direction = Cookie::get('user_direction');

    $url        = url('password/reset',$this->token);
    $subject    = trans('global.reset_password_email_subject'); 
    $greeting   = trans('global.reset_password_email_greeting'); 
    $line_01    = trans('global.reset_password_email_line_01'); 
    $action     = trans('global.reset_password_email_action'); 
    $line_02    = trans('global.reset_password_email_line_02'); 

    $view       = "notifications::email";
    if($user_language_direction == "rtl")
        $view   = "notifications::email-rtl";

    return (new MailMessage)
        ->view($view,array())
        ->subject($subject)
        ->greeting($greeting)
        ->line($line_01)
        ->action($action, $url)
        ->line($line_02);
}