如何向新用户发送通知以重置密码

时间:2019-01-16 16:26:43

标签: php laravel

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

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

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中存储功能

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 [
            //
        ];
    }
}

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

(对不起,我是乞gg)

1 个答案:

答案 0 :(得分:0)

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

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

请参见docs for notifications