Search code examples
phplaravellaravel-5middleware

Laravel make view request doesn't change in the URL


I am using middleware to redirect a user if they don't meet certain criteria (haven't accepted terms and conditions!). They are redirected to /terms.

Middleware:

public function handle($request, Closure $next)
    {
        if (Auth::user()->terms === 0) {
            return redirect()->route('getTerms');
        }

        return $next($request);
    }

The above works perfectly.

Once the user 'accepts' the terms and conditions, I want to redirect them to the homepage (which currently works), however, the URL doesn't change. It still stays on /terms, even though they're on the homepage.

Controller 'accept terms':

$user = User::find(Auth::id());
$user->terms = 1;
$user->save();

return view('home');

The reason I need the URL to redirect back to /home is because I have a layout condition, that I need a view to be included when the user is not on the /term url.

Layout condition:

 @if(!Request::is('terms'))
     @include('layouts.check')
 @endif

Why isn't the return view('home); working the same as it is in other functions?


Solution

  • Instead of returning the "home" view from your "accept terms" action, return a redirect to the actual home route.

    return redirect()->route('home');
    

    https://laravel.com/docs/5.3/responses#redirects