laravel / fortify

Backend controllers and scaffolding for Laravel authentication.

Home Page:https://laravel.com/docs/fortify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Name all routes for Ziggy support when enableViews is false

sfreytag opened this issue · comments

Fortify Version

1.16

Laravel Version

10.10

PHP Version

8.1

Database Driver & Version

No response

Description

The routes defined in https://github.com/laravel/fortify/blob/master/routes/routes.php don't always have names. The routes leading to views are always named when enableViews is true. But routes leading to backend actions used when enableViews is false are not always named.

Names can still be useful when using Ziggy to link the frontend to the Laravel route definitions. For example you can call logout by name, but not login. Here's the Fortify route definitions:

    Route::post('/login', [AuthenticatedSessionController::class, 'store'])
        ->middleware(array_filter([
            'guest:'.config('fortify.guard'),
            $limiter ? 'throttle:'.$limiter : null,
        ]));

    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
        ->name('logout');

This is similar to #468 and #470. The latter led to a draft PR #474 However imho that looks a bit complex because it attempts to assign the name two-factor.login to either the create action (if $enableViews is true) or the store action (if $enableViews is false). It seems to me an easier solution would be to leave all existing names as they are and just add new names to the unamed routes. For example in the case of two-factor.login,

        if ($enableViews) {
            Route::get('/two-factor-challenge', [TwoFactorAuthenticatedSessionController::class, 'create'])
                ->middleware(['guest:'.config('fortify.guard')])
                ->name('two-factor.login'); // Leave this as-is
        }

        Route::post('/two-factor-challenge', [TwoFactorAuthenticatedSessionController::class, 'store'])
            ->middleware(array_filter([
                'guest:'.config('fortify.guard'),
                $twoFactorLimiter ? 'throttle:'.$twoFactorLimiter : null,
            ]))
            ->name('two-factor.login.store'); // Just add a new name for this

This would not be a breaking change and would support those using Ziggy. The ultimate goal being that once using Ziggy it is more consistent to use route names throughout the frontend, rather than mixing route names and paths.

Steps To Reproduce

Install Laravel and Fortify. Set 'views' => false, in config/fortify/php/ Run php artisan route:list. Examine Fortify routes. Some are named, some not. For example,

 POST            login .......................................................... Laravel\Fortify › AuthenticatedSessionController@store
 POST            logout .............................................. logout › Laravel\Fortify › AuthenticatedSessionController@destroy

No plans for this sorry. You can use the existing names of equivalent routes

But that's my point - there are no equivalent routes with a name for me to use. There's nothing named that resolves to a POST to the url '/login' for example.