web-dev-qa-db-ja.com

Laravel 5デフォルト登録の後にメールを送信するには?

私はnoob in Laravel and with with Laravel 5.ユーザー登録とログインのために、laravelのデフォルトのシステムを使用したい。次の2つの機能で拡張します。

  1. ユーザーは登録直後にメールを受け取ります。
  2. ユーザー登録を保存したら、別のロールテーブルにエントリを作成する必要があります(ロール管理にEntrustパッケージを使用しました)

これらのことをする方法は?

16
Sovon

変更できるLaravel app/servicesにある5つのデフォルトレジストラ

<?php namespace App\Services;

    use App\User;
    use Validator;
    use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
    use Mail;

    class Registrar implements RegistrarContract {

        /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        public function validator(array $data)
        {
            return Validator::make($data, [
                'name' => 'required|max:255',
                'email' => 'required|email|max:255|unique:users',
                'password' => 'required|confirmed|min:6'
            ]);
        }

        /**
         * Create a new user instance after a valid registration.
         *
         * @param  array  $data
         * @return User
         */
        public function create(array $data)
        {
            $user = User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => \Hash::make($data['password']),
                //generates a random string that is 20 characters long
                'verification_code' => str_random(20)
            ]);

//do your role stuffs here

            //send verification mail to user
            //---------------------------------------------------------
            $data['verification_code']  = $user->verification_code;

            Mail::send('emails.welcome', $data, function($message) use ($data)
            {
                $message->from('[email protected]', "Site name");
                $message->subject("Welcome to site name");
                $message->to($data['email']);
            });


            return $user;
        }

    }

内部resources/emails/welcome.blade.php

Hey {{$name}}, Welcome to our website. <br>
Please click <a href="{!! url('/verify', ['code'=>$verification_code]) !!}"> Here</a> to confirm email

注意:検証のためにルート/コントローラーを作成する必要があります

28
Emeka Mbah

Laravelにはregistered in Illuminate\Foundation\Auth\RegistersUsersという名前の空のメソッドがあり、この操作を簡素化するために、次のようにオーバーライドします。

最初に新しい通知を追加します。

<?php

namespace App\Notifications;

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

class UserRegisteredNotification extends Notification {

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

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

    public function toMail($notifiable) {
        return (new MailMessage)
            ->success()
            ->subject('Welcome')
            ->line('Dear ' . $this->user->name . ', we are happy to see you here.')
            ->action('Go to site', url('/'))
            ->line('Please tell your friends about us.');
    }

}

このse行をRegisterController.phpに追加します。

use App\Notifications\UserRegisteredNotification;

このメソッドを追加します:

protected function registered(Request $request, $user) {
    $user->notify(new UserRegisteredNotification($user));
}

できました。

11
Sinan Eldem