laravel 5.2 重定向消息

laravel 5.2 redirect with message

如果站点用户访问/page,我希望他重定向到带有消息的索引。如何在视图中访问重定向消息?我的路线:

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');
});

我的页面控制器:

<?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");
}

重要提示:是的,我的页面已经包装在网络中间件中。并且请避免发布 Laravel 3 或 Laravel 4 解决方案。

应该是:

->with('warning', 'testing')

然后在视图中:

@if(session()->has('warning'))
    {!! session()->get('warning') !!}
@endif

user2094178 的答案是正确的,但您不必使用

->with('warning', 'testing')

它也可以是(就像你那样):

->withNews("news");
->withWarning('testing');

由于 View-class 正在使用魔法函数“__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.");
    }