【问题标题】:How to send a notification to the new user to reset password如何向新用户发送重置密码的通知
【发布时间】:2019-01-16 16:26:42
【问题描述】:

在我的项目中,只有管理员可以注册用户..一旦用户注册了一封发送给他的电子邮件,其中包含指向密码重置页面的重定向链接

我尝试使用通知来做到这一点,所以我做了:

php artisan make:notification NewUserPasswordCreate

User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laratrust\Traits\LaratrustUserTrait;
use App\Notifications\NewUserPasswordCreate;

class User extends Authenticatable 
{
    use Notifiable;
    use LaratrustUserTrait;


    protected $fillable = [
        'name', 'email', 'password',
    ];


    protected $hidden = [
        'password', 'remember_token',
    ];

    public function createAcount()
    {
        $token = app('auth.password.broker')->createToken($this);
        return $this->notify(new NewUserPasswordCreate($token));
    }
}

用户控制器中的存储功能

UserController.php

public function store(Request $request)
    {
        $this->validate($request, [
            'name'     => 'required|min:3|max:255',
            'email'    => 'required|unique:users',
            'civilNum'  => 'required|size:12',
        ]);

        if ($request->has('password') && !empty($request->password)) {
            $this->validate($request, [
                'password' => 'required|min:3|max:255',
            ]);
            $password = Hash::make($request->password);
        } else {
            $length = 10;
            $keyspace = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
            $str= '';
            $max = mb_strlen($keyspace, '8bit') - 1;
            for ($i=0; $i < $length; ++$i) { 
                $str .= $keyspace[random_int(0, $max)]; 
            }
            $password = $str;
        }
        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->civilNum = $request->civilNum;
        $user->password = $password;   

        $user->password = Hash::make($password);
        $user->save();

        if ($user->save()) {
            return redirect()->route('users.index');
        } else {
            Session::flash('danger', 'Sorry, a problem occured while creating the user.');
            return redirect()->route('users.create');
        }
    }

NewUserPasswordCreate.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class NewUserPasswordCreate extends Notification
{
    use Queueable;

    public function __construct()
    {
        //
    }

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

    public function toMail($notifiable)
    {
        $link = url( "/password/reset/?token=" . $this->token );

        return ( new MailMessage )
            ->view('reset.emailer')
            ->from('info@example.com')
            ->subject( 'Reset your password' )
            ->line( "Hey, We've successfully changed the text " )
            ->action( 'Reset Password', $link )
            ->attach('reset.attachment')
            ->line( 'Thank you!' );
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

我不知道如何使它工作..谢谢。

(对不起..我是初学者)

【问题讨论】:

  • 检查the documentation。有一个指南可以完成that
  • 我需要帮助来编辑我的代码..谢谢
  • $this-&gt;token 在您的 toMail 函数中未定义。此外,您需要确保您有一个位于resources/views/reset/emailer.blade.php 的模板,因为您有-&gt;view('reset.emailer')
  • 很好..保存用户后UserController中的代码是什么?

标签: php laravel


【解决方案1】:

为了通知用户,您需要在具有Notifiable 特征的User 对象上调用notify() 方法。

$user = User::find($id);
$user->notify(new App\Notifications\NewUserPasswordCreate);

请参阅docs for notifications

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 2020-03-19
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    相关资源
    最近更新 更多