Laravel 9 Custom Email Verification Tutorial
- Create a new Laravel project using the following command:
composer create-project --prefer-dist laravel/laravel my_project
- Run the database migrations using the following command:
php artisan migrate
- Create a new middleware using the following command:
php artisan make:middleware EmailVerified
- Open the
EmailVerified
middleware in theapp/Http/Middleware
directory and add the following code:
phppublic function handle($request, Closure $next)
{
if (!$request->user()->email_verified_at) {
return redirect()->route('verification.notice');
}
return $next($request);
}
- Create a new controller using the following command:
php artisan make:controller VerificationController
- Open the
VerificationController
in theapp/Http/Controllers
directory and add the following code:
csharppublic function notice()
{
return view('verification.notice');
}
- Create a new view using the following command:
php artisan make:view verification.notice
- Open the view file in the
resources/views/verification
directory and add the following code:
css<h1>Please verify your email address</h1>
- Add a new route in the
routes/web.php
file:
cssRoute::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');
});
- Add the middleware to the
$routeMiddleware
property in theapp/Http/Kernel.php
file:
phpprotected $routeMiddleware = [
// ...
'email_verified' => \App\Http\Middleware\EmailVerified::class,
];
- Finally, add the email verification logic to the registration process in the
register
method of theAuth\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
Post a Comment