Tutorial Laravel Fortify - 01 Menginstal Fortify

3 min read 5 months ago
Published on Oct 29, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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:

  1. Install Laravel (if not already installed):

    • Use Composer to create a new Laravel project:
      composer create-project --prefer-dist laravel/laravel your-project-name
      
  2. Navigate to Your Project Directory:

    • Change directories to your new Laravel project:
      cd your-project-name
      
  3. 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.

  1. Run the Composer Command:

    • Execute the following command in your terminal:
      composer require laravel/fortify
      
  2. Publish Fortify's Configuration:

    • Publish the Fortify configuration file with this command:
      php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
      
  3. Review the Configuration File:

    • After publishing, check the config/fortify.php file to customize authentication features as needed.

Step 3: Set Up Fortify Service Provider

To ensure Fortify is registered, you need to add its service provider to your application.

  1. Open config/app.php:

    • Add the Fortify service provider to the providers array:
      Laravel\Fortify\FortifyServiceProvider::class,
      
  2. Register Fortify Features:

    • In the boot method of your App\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
      }
      

Step 4: Configure Authentication Views

Fortify does not come with pre-built authentication views. You need to create your own.

  1. Create Authentication Views:

    • Build your login and registration views in the resources/views/auth directory. For example, create login.blade.php and register.blade.php.
  2. 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.

  1. Create the User Table Migration:

    • If you haven't already, run the migration command:
      php artisan migrate
      
  2. Check Your Database:

    • Ensure that the users table is created in your database.

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.