Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dingo_d's avatar

Redirect session message triggered on back button

I am following this tutorial for creating user roles, views, etc. And I've created all the views, the controller and all that jazz.

In my controller I've added a 'guard' clause for the show() method - if somebody were to try to access the ID of the user that doesn't exist, I'd redirect them to the users screen with a dismissable warning.

public function show($id)
{
    $user = User::find($id);

    $view = redirect()->route('users.index')
        ->with('userNotFound', "User with the ID {$id} doesn't exist.");

    if ($user) {
        $view = view('users.show', compact('user'));
    }

    return $view;
}

In my users/show.blade.php I have a 'back' button <a class="btn btn-primary" href="{{ route('users.index') }}">Back</a>, and when I'd click it, I'd be sent back to the users/index.blade.php screen with the notice that the ID of the user (on whose screen I was just a second ago) doesn't exist.

Which was odd. So I've placed the $view variable in the else part of the conditional and all is well

public function show($id)
{
    $user = User::find($id);


    if ($user) {
        $view = view('users.show', compact('user'));
    } else {
        $view = redirect()->route('users.index')
            ->with('userNotFound', "User with the ID {$id} doesn't exist.");
    }

    return $view;
}

Notice won't be shown when I click the back button.

The notice was outputted like this:

@if ($message = session('success'))
    <div class="alert alert-success">
        {{ $message }}
    </div>
@elseif($message = session('userNotFound'))
    <div class="alert alert-warning alert-dismissible fade show">
        {{ $message }}
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
@endif

in my users/index.blade.php

I've stored the redirect and the view in a variable to avoid using multiple returns.

But what bothers me is: why was this happening in the first place?

The back button will make a request towards the users.index route which is governed by the index() method in my UserController, the redirect part which is in the show() method should never been invoked.

Why did this happen and what am I missing?

0 likes
4 replies
Nakov's avatar
Nakov
Best Answer
Level 73

This should be simpler I think:

public function show($id)
{
    $user = User::find($id);

    if ($user) {
        return view('users.show', compact('user'));
    }

    return redirect()->route('users.index')
            ->with('userNotFound', "User with the ID {$id} doesn't exist.");
}

And now what is the problem that you are experiencing, I don't understand :)

But you can add a breakpoint in your index method using dd() helper and make sure you are getting there.

dingo_d's avatar

I was avoiding writing multiple returns in my method. And it worked (until I added the back button :D). Somehow this redirect was picked up by the session when I clicked on it which was weird. I also tried logging to see if the show() method was invoked when I clicked the back button, but it wasn't. Which made things super weirder 🤷‍♂️

Nakov's avatar

@dingo_d there is nothing wrong with having multiple returns. In your case you call the variable $view but it is not view both times, once it is just a redirect :) so that's not cleaner then what I use, and I got rid of the else as it is not needed.

So do you put userNotFound in another method as well, because with() uses a flash session which lasts for one request only. So unless the back button in your case just refreshes the page which then preserved the session, then I don't see how it is possible any other way.

1 like
dingo_d's avatar

I'm not using userNotFound in another method, but you do have a point with the variable, I was looking at the complexity from a perspective of some automated scans, instead of looking at code as something that should be understandable when you read it.

I'll use the multiple returns, since that does the trick.

Thanks!

Please or to participate in this conversation.