Laravel 9 Custom Email Verification Tutorial

  1. Create a new Laravel project using the following command: composer create-project --prefer-dist laravel/laravel my_project
  2. Run the database migrations using the following command: php artisan migrate
  3. Create a new middleware using the following command: php artisan make:middleware EmailVerified
  4. Open the EmailVerified middleware in the app/Http/Middleware directory and add the following code:
php
public function handle($request, Closure $next) { if (!$request->user()->email_verified_at) { return redirect()->route('verification.notice'); } return $next($request); }
  1. Create a new controller using the following command: php artisan make:controller VerificationController
  2. Open the VerificationController in the app/Http/Controllers directory and add the following code:
csharp
public function notice() { return view('verification.notice'); }
  1. Create a new view using the following command: php artisan make:view verification.notice
  2. Open the view file in the resources/views/verification directory and add the following code:
css
<h1>Please verify your email address</h1>
  1. Add a new route in the routes/web.php file:
css
Route::middleware(['auth', 'email_verified'])->group(function () { Route::get('/dashboard', function () { return view('dashboard'); }); }); Route::middleware(['auth'])->group(function () { Route::get('email/verify', 'VerificationController@notice')->name('verification.notice'); });
  1. Add the middleware to the $routeMiddleware property in the app/Http/Kernel.php file:
php
protected $routeMiddleware = [ // ... 'email_verified' => \App\Http\Middleware\EmailVerified::class, ];
  1. Finally, add the email verification logic to the registration process in the register method of the Auth\RegisterController class.

This tutorial should give you a good starting point for creating custom email verification in Laravel 9. Keep in mind that you may need to make additional modifications to meet your specific requirements.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755