vendredi 17 avril 2020

Laravel redirect()->send() does not stop app completely

I might get this entirely wrong, but I was under the impression that redirect()->send() would force the app to stop and redirect the user. As it turns out, it kind of does that, but not entirely.

Let's assume we have the following function.

function validate()
{
    $validator = ... // a working implementation

    if ($validator->fails()) {
        redirect()->back()->withErrors($validator)->withInput()->with(['_type' => $type])->send();
    }
}

This function can be called during the execution of a request. If it succeeds, nothing should happen.

An example use case is this:

function something(Request $request)
{
    $this->validate();
    \Log::debug('Running like a mad man!');
}

If validate() doesn't fail, it should log a debug message. If it does fail we would like it to redirect.

We could off course add logic in something() but we prefer not to.

In our case, Log::debug, always runs. At first I thought it only happened in Laravel 5.8, but apparently we have this issue in older versions too.

You can't use die() or similar after the redirect as that returns a 419. I am not up to speed with the Laravel internals but I am guessing it doesn't get the chance to set it's session.

Is this something we can solve without adding logic in something()?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire