Php 在Laravel 5.4中定制忘记密码的电子邮件

Php 在Laravel 5.4中定制忘记密码的电子邮件,php,laravel,Php,Laravel,我正在尝试在Laravel中自定义密码重置电子邮件 我必须重写此函数: namespace Illuminate\Auth\Passwords; use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification; use Illuminate\Http\Request; trait CanResetPassword { /** * Get the e-mail address wher

我正在尝试在Laravel中自定义密码重置电子邮件

我必须重写此函数:

namespace Illuminate\Auth\Passwords;

use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
use Illuminate\Http\Request;


trait CanResetPassword
{
    /**
     * Get the e-mail address where password reset links are sent.
     *
     * @return string
     */
    public function getEmailForPasswordReset()
    {
        return $this->email;
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */

public function sendPasswordResetNotification($token)
{

    $this->notify(new ResetPasswordNotification($token));

}
这是我的尝试:

 public function sendPasswordResetNotification($token, Requests $request)
{
Mail::to($request->email)->send(new newpassword($token));
}
我得到这个错误:

声明 Illumb\Foundation\Auth\User::sendPasswordResetNotification($token, Illumb\Http\Request$Request)必须与兼容 Illumb\Contracts\Auth\CanResetPassword::sendPasswordResetNotification($token)


如果您阅读了错误,则表明您的类与
CanResetPassword
不兼容。如果你看那个

interface CanResetPassword
{
    /**
     * Get the e-mail address where password reset links are sent.
     *
     * @return string
     */
    public function getEmailForPasswordReset();
    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token);
}
您可以看到函数
sendPasswordResetNotification
应该只包含一个参数,
$token
。因此,您需要将
Request$Request
作为参数从方法签名中删除

为了获取请求,您需要在
sendPasswordResetNotification
方法中使用函数
request()

public function sendPasswordResetNotification($token)
{
    Mail::to(request()->email)->send(new newpassword($token));
}

我很惊讶你会这么长时间来定制电子邮件

请尝试以下方法:

php artisan vendor:publish
然后在这里修改文件

/resources/views/vendor/notifications/email.blade.php
非常适合我们使用

user@default:~/laravel_5.4$ php artisan vendor:publish
Copied Directory [/vendor/laravel/framework/src/Illuminate/Pagination/resources/views] To [/resources/views/vendor/pagination]
Copied Directory [/vendor/laravel/framework/src/Illuminate/Notifications/resources/views] To [/resources/views/vendor/notifications]
Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail]
Publishing complete.
现在,如果需要更改副本,并且需要原始ResetPassword类使用的奇特按钮,可以在User.php类中扩展mail类,如下面的示例所示

以下是我们的一份非常好的示例:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Notifications\Messages\MailMessage;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'Users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'firstName',
        'lastName',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Sends the password reset notification.
     *
     * @param  string $token
     *
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new CustomPassword($token));
    }
}

class CustomPassword extends ResetPassword
{
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('We are sending this email because we recieved a forgot password request.')
            ->action('Reset Password', url(config('app.url') . route('password.reset', $this->token, false)))
            ->line('If you did not request a password reset, no further action is required. Please contact us if you did not submit this request.');
    }
}

查看此答案您也确实解决了此问题,非常好谢谢您的解决方案。为了改进您的答案,您可以使用
php-artisan-make:notification-CustomPassword
命令来创建这个类的脚手架。