Tutorial Laravel Fortify - 01 Menginstal Fortify
Table of Contents
Introduction
In this tutorial, we will learn how to install Laravel Fortify, a package that provides authentication features for Laravel applications. By the end of this guide, you will have a solid understanding of the installation process and how to set up Fortify in your Laravel project.
Step 1: Prepare Your Laravel Environment
Before installing Fortify, ensure you have a Laravel project set up. If you haven't created a Laravel application yet, follow these steps:
-
Install Laravel (if not already installed):
- Use Composer to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel your-project-name
- Use Composer to create a new Laravel project:
-
Navigate to Your Project Directory:
- Change directories to your new Laravel project:
cd your-project-name
- Change directories to your new Laravel project:
-
Ensure Composer is Installed:
- Verify that Composer is installed on your system to manage dependencies.
Step 2: Install Laravel Fortify
Next, you will install the Laravel Fortify package.
-
Run the Composer Command:
- Execute the following command in your terminal:
composer require laravel/fortify
- Execute the following command in your terminal:
-
Publish Fortify's Configuration:
- Publish the Fortify configuration file with this command:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
- Publish the Fortify configuration file with this command:
-
Review the Configuration File:
- After publishing, check the
config/fortify.php
file to customize authentication features as needed.
- After publishing, check the
Step 3: Set Up Fortify Service Provider
To ensure Fortify is registered, you need to add its service provider to your application.
-
Open
config/app.php
:- Add the Fortify service provider to the
providers
array:Laravel\Fortify\FortifyServiceProvider::class,
- Add the Fortify service provider to the
-
Register Fortify Features:
- In the
boot
method of yourApp\Providers\FortifyServiceProvider
, register the desired features:use Laravel\Fortify\Fortify; public function boot() { Fortify::createUsersUsing(CreateNewUser::class); Fortify::loginView(fn() => view('auth.login')); // Add other features as necessary }
- In the
Step 4: Configure Authentication Views
Fortify does not come with pre-built authentication views. You need to create your own.
-
Create Authentication Views:
- Build your login and registration views in the
resources/views/auth
directory. For example, createlogin.blade.php
andregister.blade.php
.
- Build your login and registration views in the
-
Design Your Views:
- Use Laravel Blade syntax to create forms for user login and registration.
Step 5: Run Database Migrations
In order to store user data, run the necessary migrations.
-
Create the User Table Migration:
- If you haven't already, run the migration command:
php artisan migrate
- If you haven't already, run the migration command:
-
Check Your Database:
- Ensure that the
users
table is created in your database.
- Ensure that the
Conclusion
You have successfully installed and set up Laravel Fortify in your application. This tutorial walked you through preparing your Laravel environment, installing the package, configuring it, and creating necessary views.
Next Steps
- Explore additional Fortify features like password reset and email verification.
- Customize your authentication views further to match your application's design.
- Consider implementing middleware for route protection based on authentication status.
With this knowledge, you are well on your way to enhancing your Laravel application's authentication capabilities.