Laravelメールの検証を無効にするとワンタッチで有効にします

lovecoding:

ユーザーは、登録時にとにかく、我々は動的に有効/無効にメールの確認を設定することができますがありますか?

私は、設定された電子メールの確認するために、考えて、我々は設定する必要がありますAuth::routes(['verify' => true]);し、Userモデルに、我々は設定する必要がありますclass User extends Authenticatable implements MustVerifyEmail

これを動的に行うための任意の簡単な方法はありますか?それでは、私たちは有効/無効管理パネルで設定したとしましょう。そして、それによると、私たちは、有効/無効のフロント認証ページに設定することができますか?

黙示録Kristianto:

あなたは、単に操作できるemail_verified_atの列usersのテーブルを。

私は、オブザーバを使用することをお勧め:

php artisan make:observer UserObserver --model=User

あなたは確認メールを使用するかどうかを判断するために設定するか、データベースを使用することができます。

UserObserver

class UserObserver
{
    /**
     * Handle the user "created" event.
     *
     * @param  \App\User  $user
     * @return void
     */
    public function created(User $user)
    {
        // Let's say you use config

        if (config('app.email_verification') == false) {
            $user->email_verified_at = now();
            $user->save();
        }
    }

    //

}

メール配信を決定するには、上書きすることができsendEmailVerificationNotification、ユーザーのモデルに:

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    if (config('app.email_verification')) {
        $this->notify(new VerifyEmail);
    }
}

更新

保つためにemail_verified_atとしてnull、あなたがオブザーバーを削除することができ、その後、あなたの更新Auth::routes

web.php

Auth::routes([
    'verify' => config('app.email_verification')
]);


Route::group([
    'middleware' => [config('app.email_verification') ? 'verified' : null]
], function () {

    // protected routes

    Route::get('dashboard', 'DashboardController@index');

});

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=347187&siteId=1
おすすめ