Reputation: 5462
if user of site visits /page, I want him to redirect to index with message. How do I access redirect message in view? My routes:
Route::group(['middleware' => 'web'],function(){
Route::get('/page', function () {
return redirect('/')->withMessage(["warning"=> ["yeah","test"]]); // According official docs, this should work.
});
Route::get('/', 'Page@front');
});
My Page controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\News;
use App\PageContact;
use Session;
class Page extends Controller{
public function front(Request $Request){
return view('index')->withNews("news");
}
important: yes, my pages are already wrapped in web middleware. And please avoid posting Laravel 3 or Laravel 4 solutions.
Upvotes: 2
Views: 4916
Reputation: 166
The answer from user2094178 is correct, but you don't have to use
->with('warning', 'testing')
it can also be (as you did):
->withNews("news");
->withWarning('testing');
Since the View-class is using the magic function "__call":
/**
* Dynamically bind parameters to the view.
*
* @param string $method
* @param array $parameters
* @return \Illuminate\View\View
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (Str::startsWith($method, 'with')) {
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
}
throw new BadMethodCallException("Method [$method] does not exist on view.");
}
Upvotes: 0
Reputation: 9444
It should be:
->with('warning', 'testing')
Then in the view:
@if(session()->has('warning'))
{!! session()->get('warning') !!}
@endif
Upvotes: 6