Čamo's avatar
Level 3

back()->withErrors() vs. redirect->route()->withErrors()

I have a admin middleware which check out if user is logged in. If he is not I want to redirect him to login page. But in login form (it is modal on articles page) I would like to show error with message. So I tried to call:

return redirect()->route('articles')->withErros(['login' => 'You have to log in to access admin module.']);

This does not show the error cause withErrors is not a metohod in RedirectResponse object. Can somebody tell me what is the difference between back() and redirect()->route()? Cause as I see

return back()->withErrors(['login' => 'You have to log in to access admin module.']);

works well and I am able to show errors in login form.

Here is the whole middleware

	public function handle(Request $request, Closure $next)
	{
		$user = $request->user();

		if( !$user )
		{
			session()->flash('showModal', 'loginModal');
			// This does not work
			return redirect()->route('articles')->withErros(['login' => 'You have to log in to access admin module.']);
			// This works but is is restricted to back url...
			return back()->withErrors(['login' => 'You have to log in to access admin module.']);
		}
		elseif( !$user->hasRole([Role::ROLE_ADMIN, Role::ROLE_REDACTOR]) )
		{
			flash('You do not have permission do access admin module.')->error()->important();
			return redirect()->route('articles');
		}

		return $next($request);
	}
0 likes
11 replies
Nakov's avatar

withErrors adds the errors in a flash session which gets lost after the first request is performed. So essentially, back()->withErrors() goes back one level, and that's why the flash session is still valid and you can see the errors, while once you do redirect()->route() it goes to another action, in which the errors will be available, but then from there you are returning a different redirect or view, in which case the flash sessions is being flashed/removed. So that's why you won't be able to see the errors.

Hope this helps.

2 likes
Čamo's avatar
Level 3

I understand the redirect but would like to create custom error flashes. But it seems it is little complicated as I see in withError()

    public function withErrors($provider, $key = 'default')
    {
        $value = $this->parseErrors($provider);

        $errors = $this->session->get('errors', new ViewErrorBag);

        if (! $errors instanceof ViewErrorBag) {
            $errors = new ViewErrorBag;
        }

        $this->session->flash(
            'errors', $errors->put($key, $value)
        );

        return $this;
    }
Nakov's avatar

This

$this->session->flash

is what makes it go away after one request. Using $this->session->put will work, but then you will have to be responsible for clearing it yourself, otherwise you will see error messages all the time. So i won't go that road.

1 like
Čamo's avatar
Level 3

I am sorry the whole problem was the typo. redirect()->route('articles')->withErrors() works. I am sorry for that.

shaungbhone's avatar
Level 28

Have u solved this? You missed spelling in this line. *withErros

return redirect()->route('articles')->withErros(['login' => 'You have to log in to access admin module.']);
1 like
Čamo's avatar
Level 3

You are right. There was a typo. Thanks a lot. Till now I was thinking Laravel is not able to send errors from redirect()->route().

1 like
ep!sode's avatar

It's a bit tricky but here are ways on how to use these redirection options:

back()->withErrors() => redirects to the previous route with errors
redirect()->route(...)->withErrors() is quite wrong. This should be redirect(route(...))->withErrors()

If you want custom flash data, you cab go with: redirect()->route(...)->with('key', 'value). This will flash the error once on the view. After a page reload, this session will be flushed.

1 like
Čamo's avatar
Level 3

Why do you think redirect()->route(...)->withErrors() is quite wrong? The problem was in typo. I am sorry for that.

xewl's avatar

I'd argue chaining the route from the same class, rather than giving a helper as an option/parameter is quite a lot more right tho'. That's what chaining's for :D

ep!sode's avatar

Hmm. I have this issue before that flashing session in view resulted in error. I have returned redirect()->route()->with('key', 'value') before. Perhaps it's just me lmaoo. Or perhaps I did not noticed that withErrors() was chained instead of with().

Please or to participate in this conversation.