PHP code example of stylers / laravel-email-verification

1. Go to this page and download the library: Download stylers/laravel-email-verification library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stylers / laravel-email-verification example snippets


use Stylers\EmailVerification\NotifiableInterface;
use Illuminate\Notifications\Notifiable;
use Stylers\EmailVerification\EmailVerifiableInterface;
use Stylers\EmailVerification\Frameworks\Laravel\Models\Traits\EmailVerifiable;

class User extends Model implements NotifiableInterface, EmailVerifiableInterface
{
    use Notifiable;
    use EmailVerifiable;
    ...
    public function getName(): string
    {
        return (string)$this->name;
    }
}

// app/Providers/EventServiceProvider.php
protected $listen = [
    ...
    'Stylers\EmailVerification\Frameworks\Laravel\Events\VerificationSuccess' => [
        'your\listener\class1',
        'your\listener\class2',
    ]
];
// OR you can register your listener via Event facade in any ServiceProvider::boot method
Event::listen(
    'Stylers\EmailVerification\Frameworks\Laravel\Events\VerificationSuccess',
    'your\listener\class'
);

Route::post('/email-verification-request', 'AnyController@createEmailVerificationRequest')
    ->name('email-verification-request.create');

use Stylers\EmailVerification\Frameworks\Laravel\Notifications\EmailVerificationRequestCreate;
use Stylers\EmailVerification\Exceptions\AlreadyVerifiedException;
...
class AnyController extends Controller {
    ...
    public function createRequest(
        Request $request, 
        EmailVerificationRequestInterface $emailVerificationService
    )
    {
        $verifiableUser = $notifiableUser = User::first();
        try {
            $verificationRequest = $emailVerificationService->createRequest($verifiableUser->email);
            $emailVerificationService->sendEmail($verificationRequest->getToken(), $notifiableUser);
        } catch (AlreadyVerifiedException $e) {
            // handle exception
        }

        ...
    }
}



Route::get('/email/verify/{token}', 'AnyController@verifyEmail')
    ->name(config('email-verification.route'));

...
use Stylers\EmailVerification\Exceptions\ExpiredVerificationException;
use Stylers\EmailVerification\Exceptions\AlreadyVerifiedException;
use Stylers\EmailVerification\EmailVerificationServiceInterface;
...
class AnyController extends Controller {
    ...
    public function verifyEmail(
        Request $request, 
        EmailVerificationServiceInterface $emailVerificationService
    )
    {
        $token = $request->input('token');
        try {
            $emailVerificationRequest = $emailVerificationService->verify($token);
        } catch(ExpiredVerificationException $e) {
            // expired verification token
        } catch(AlreadyVerifiedException $e) {
            // already verified email
        } catch(\InvalidArgumentException $e) {
            // non-existing token
        }
        ...
        // email verification succeed
    }
}
bash
php artisan vendor:publish --tag=laravel-mail
php artisan vendor:publish --provider="Themsaid\MailPreview\MailPreviewServiceProvider"
php artisan vendor:publish --provider="Stylers\EmailVerification\Frameworks\Laravel\ServiceProvider"
bash
php artisan migrate