Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to view but change URL in Laravel

I'm trying to redirect the user after the create wizard is complete back to the main page.

If I write in my controller return view('main')->with('key'=>'data'), all is fine, but the URL does not change to localhost:8080 but it stays as 'localhost:8080/wizard/finish`.

If I use redirect('/')->with(['message' => 'Done.']) it redirects to the main page, but because there is already a Route::get('/','Controller'), the controller is triggered and it returns my default main page with no message.

 <div class="row">
            <div class="col-12">
                @if (isset($message))
                    <p align="center" style="color: red;"><strong>{{$message}}</strong></p>
                @endif
            </div>
        </div>

EDIT: With a breakpoint in the MainPageController (mapped to /), I see that this controller is triggered when there is a redirect to the / route. Thus, I lose the $message, as the MainPageController also returns the same view, but with no message.

like image 427
Alexandru Antochi Avatar asked Jun 09 '17 08:06

Alexandru Antochi


2 Answers

Try this

 return redirect('/url')->with('var', 'value');
like image 180
Vishal Varshney Avatar answered Sep 22 '22 13:09

Vishal Varshney


When you're redirecting, Laravel uses the session to keep the message between requests.

return redirect('/')->with('message', 'Done.');

So, to display the message change this:

@if (isset($message))
    <p align="center" style="color: red;"><strong>{{$message}}</strong></p>
@endif

To this:

@if (session()->has('message'))
    <p align="center" style="color: red;"><strong>{{ session('message') }}</strong></p>
@endif
like image 42
Alexey Mezenin Avatar answered Sep 19 '22 13:09

Alexey Mezenin