How to change “/login” url with Fortify?

4.7k Views Asked by At

In laravel 9 with Fortify Authentication I see in routes

  GET|HEAD        login ................................................................................................................................................... login › Laravel\Fortify › AuthenticatedSessionController@create
  POST            login .............................................................................................................................. generated::kNTXAE1j2bp8Zq09 › Laravel\Fortify › AuthenticatedSessionController@store

and my login page is opened by “/login” url.

How can I to make this login as “/admin/login” url?

Leaving all Fortify functionality as it was before?

Thanks!

3

There are 3 best solutions below

1
On BEST ANSWER

In your FortifyServiceProvider in the boot() method, you should add

Fortify::ignoreRoutes();

before the default configuration.

It will tell Fortify to ignore the build-in routes.

Then you should copy Fortify's routes (./vendor/laravel/fortify/routes/routes.php) to your own routes (./routes/web.php) file.

Then in your web.php file you can make changes accordingly.

0
On

There is also a more personalized option where you can rename any route separately.

Add the following lines in config/fortify.php:

 return [
      'paths' => [
        'login' => '/admin/login'
       ],   
  ];

You can do this with all named routes,

Sorry for the rusty English :)

1
On

For my case:

Laravel Framework 9.25.1 and "laravel/jetstream": "^2.11" which contains "laravel/fortify": "^1.13.3"

  1. Prefix /admin may add in config/fortify.php file. In this file find prefix key and add you prefix. Example:
return [
    // code...
    'prefix' => 'admin',
    // code...
];
  1. I add to app/Providers/FortifyServiceProvider.php for register method:
public function register()
{
    Fortify::ignoreRoutes(); // add this string
    // ... next code
}

for boot method:

public function boot()
{
    Route::group([
       'namespace' => 'Laravel\Fortify\Http\Controllers',
       'domain' => config('fortify.domain', null),
       'prefix' => config('fortify.prefix'),
    ], function () {
        $this->loadRoutesFrom(base_path('routes/fortify.php'));
    }); // this closure

    // ... next code
}
  1. Copy vendor/laravel/fortify/routes/routes.php to routes/fortify.php and change routes in this file. The same path is specified in my closure in app/Providers/FortifyServiceProvider@boot.