2016-11-13 32 views
10

Tôi là người mới bắt đầu ở Laravel. Hiện tại tôi đang học khuôn khổ này. Phiên bản Laravel của tôi là 5.3.Làm thế nào để thay đổi đặt lại mật khẩu email chủ đề trong laravel?

Tôi đang dàn dựng xác thực của mình bằng cách sử dụng php artisan make:auth Tất cả đều hoạt động tốt. Ngoài ra tôi đã cấu hình gmail smtp trong tập tin .env và mail.php của tôi trong cấu hình directgory. Tất cả đều hoạt động hoàn hảo. Nhưng tôi thấy theo mặc định, chủ đề email bị quên mật khẩu sẽ là Reset Password. Tôi muốn thay đổi điều đó.

Tôi đã xem một số blog. Tôi tìm thấy một số blog. Tôi đã thực hiện điều đó trong trang web của mình. Nhưng cùng một đầu ra sắp tới.

Tôi đi theo những liên kết này -

https://laracasts.com/discuss/channels/general-discussion/laravel-5-password-reset-link-subject

https://laracasts.com/discuss/channels/general-discussion/reset-password-email-subject

https://laracasts.com/discuss/channels/laravel/how-to-override-message-in-sendresetlinkemail-in-forgotpasswordcontroller

Trả lời

25

Bạn có thể thay đổi chủ đề email đặt lại mật khẩu của mình nhưng sẽ cần thêm một số công việc. Trước tiên, bạn cần tạo triển khai của riêng mình cho thông báo ResetPassword.

Tạo một lớp thông báo mới bên app\Notifications thư mục, chúng ta hãy đặt tên nó ResetPassword.php:

<?php 

namespace App\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) 
    { 
     return (new MailMessage) 
      ->subject('Your Reset Password Subject Here') 
      ->line('You are receiving this email because we received a password reset request for your account.') 
      ->action('Reset Password', url('password/reset', $this->token)) 
      ->line('If you did not request a password reset, no further action is required.'); 
    } 
} 

Bạn cũng có thể tạo ra các mẫu thông báo sử dụng lệnh thợ thủ công:

php artisan make:notification ResetPassword 

Hoặc bạn chỉ có thể copy-paste mã trên. Như bạn có thể thấy lớp thông báo này khá giống với lớp mặc định Illuminate\Auth\Notifications\ResetPassword. Bạn thực sự có thể mở rộng nó từ lớp mặc định ResetPassword.

Sự khác biệt duy nhất là ở đây, bạn thêm một lời gọi phương thức mới để xác định chủ đề của email:

return (new MailMessage) 
     ->subject('Your Reset Password Subject Here') 

Bạn có thể đọc thêm về Mail Notifications here.

Thứ hai, trên tệp app\User.php, bạn cần ghi đè phương thức mặc định sendPasswordResetNotification() được xác định bởi Illuminate\Auth\Passwords\CanResetPassword đặc điểm. Bây giờ bạn nên sử dụng triển khai ResetPassword của riêng mình:

<?php 

namespace App; 

use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 
use App\Notifications\ResetPassword as ResetPasswordNotification; 

class User extends Authenticatable 
{ 
    use Notifiable; 

    ... 

    public function sendPasswordResetNotification($token) 
    { 
     // Your your own implementation. 
     $this->notify(new ResetPasswordNotification($token)); 
    } 
} 

Và bây giờ, chủ đề email đặt lại mật khẩu của bạn sẽ được cập nhật!

Reset password email subject updated

Hy vọng trợ giúp này!

+0

và làm thế nào chúng ta có thể thay đổi liên quan đến Laravel và Laravel được viết ở trên cùng. – Steve

+1

@Steve Chuyển đến config/app.php và thay đổi tên ứng dụng – kniteli

1

Bạn dễ dàng có thể sửa đổi các lớp thông báo sử dụng để gửi liên kết đặt lại mật khẩu cho người dùng. Để bắt đầu, hãy ghi đè phương thức sendPasswordResetNotification trên Mô hình người dùng của bạn. Trong phương thức này, bạn có thể gửi thông báo bằng cách sử dụng bất kỳ lớp thông báo nào bạn chọn. Mật khẩu reset $token là đối số đầu tiên đã nhận được bằng phương pháp này, Xem Doc for Customization

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

Hope this helps!

+0

đơn giản hơn nhiều rồi được chấp nhận ..! –

2

Bạn có thể tạo chức năng tùy chỉnh sẽ tạo mã thông báo đặt lại mật khẩu như thế này.

$user = User::where('email', '[email protected]')->first(); 
$password_broker = app(PasswordBroker::class); //so we can have dependency injection 
$token = $password_broker->createToken($user); //create reset password token 
$password_broker->emailResetLink($user, $token, function (Message $message) { 
     $message->subject('Custom Email title'); 
});//send email. 
0

Chỉ cần thêm dòng:

-> chủ đề ('New Subjetc')

trong phương pháp toMail của tập tin Illuminate \ Auth \ Notifications \ ResetPassword như thế này:

public function toMail($notifiable) 
{ 
    return (new MailMessage) 
     ->subject('New Subjetc') 
     ->line('You are receiving this email because we received a password reset request for your account.') 
     ->action('Restaurar Contraseña', url(config('app.url').route('password.reset', $this->token, false))) 
     ->line('If you did not request a password reset, no further action is required.'); 
} 
Các vấn đề liên quan