laravel / fortify

Backend controllers and scaffolding for Laravel authentication.
https://laravel.com/docs/fortify
MIT License
1.62k stars 294 forks source link

Allow Fortify::xxView to render custom responses #33

Closed hailwood closed 4 years ago

hailwood commented 4 years ago

Right now if we want to change the login view we can do

Fortify::loginView('different-view')

But that only allows you to change it to a different view name, behind the scenes it's just rendering a singleton binding for LoginViewResponse.

You'd think no stress, we can override that binding for example for an Inertia view

$this->app->singleton(LoginViewResponse::class, () => Inertia::render('Login'));

But unfortunately this doesn't work due to the typehint on the controller method

So instead the next best option is resolving an anonymous class that proxies the inertia response

$this->app->singleton(LoginViewResponse::class, function () {
    return new class implements LoginViewResponse
    {
        public function toResponse($request)
        {
            return Inertia::render('Login')->toResponse($request);
        }
    };
});

This works perfectly but isn't exactly friendly. My suggestion is that we loosen the typehint to just Response and allow Fortify::loginView to take a closure which would be used as the binding, meaning it would be as simple as

Fortify::loginView(() => Inertia::render('Login'));

Of course we'd do the same for the other view methods. I'm happy to PR this if the idea gets approved.

hailwood commented 4 years ago

https://blog.laravel.com/jetstream-customization-and-password-confirmation

stephensamra commented 4 years ago

https://blog.laravel.com/jetstream-customization-and-password-confirmation

Is there a solution (other than the anonymous class workaround above) if you're using Fortify without Jetstream?