Laravel 4 - 使用 Flash 消息重定向不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22387844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - Redirect with flash message not working
提问by blazej
In my Laravel 4 project, I am trying to set a flash message when redirecting, like so:
在我的 Laravel 4 项目中,我试图在重定向时设置一个 flash 消息,如下所示:
return Redirect::route('main')->with('flash', 'Message here.');
(I looked at the following: Laravel 4 - Redirect::route with parameters).
(我查看了以下内容:Laravel 4 - Redirect::route with parameters)。
The redirect happens properly. In my 'main' view I've got this code to display the flash message:
重定向正确发生。在我的“主”视图中,我有这个代码来显示 flash 消息:
@if (Session::has('flash'))
<div class="flash alert">
<p>{{ Session::get('flash') }}</p>
</div>
@endif
But it doesn't work: Session::get('flash')
returns an array, not a string. If I insert a call to var_dump(Session::get('flash'))
, I get this output:
但它不起作用:Session::get('flash')
返回一个数组,而不是一个字符串。如果我插入对 的调用var_dump(Session::get('flash'))
,则会得到以下输出:
array (size=2)
'new' =>
array (size=0)
empty
'old' =>
array (size=1)
0 => string 'flash' (length=5)
What is happening? It seems like fairly straightforward code...
怎么了?这似乎是相当简单的代码......
回答by Jeff Lambert
When you redirect with data, the data you save will be 'flashed' into session (meaning it will only be available during the next request, see: http://laravel.com/docs/session#flash-data).
当您使用数据重定向时,您保存的数据将“闪现”到会话中(这意味着它仅在下一个请求期间可用,请参阅:http: //laravel.com/docs/session#flash-data)。
I would be wary of using 'flash'
as a flash data key, as Laravel stores its flash data in a key already with a name of 'flash'
. Try changing your name to something else. You may be confusing Laravel's Session::get
as to where it should be attempting to load the data from.
我对'flash'
用作闪存数据密钥持谨慎态度,因为 Laravel 将其闪存数据存储在一个名称为'flash'
. 尝试将您的名字更改为其他名称。您可能对 LaravelSession::get
应该尝试从何处加载数据感到困惑。
回答by Abdul Rehman Ch
Inside of the routes.php file try to create your routes within the
在 routes.php 文件中尝试在
Route::group(['middleware' => ['web']], function () {
Route::get('/', [
'as' => 'home',
'uses' => 'PagesController@home'
]);
Route::resource('tasks', 'TasksController');
});
});
And After that use.
然后使用。
@if (Session::has('flash'))
<div class="flash alert">
<p>{{ Session::get('flash') }}</p>
</div>
@endif
Where you need to access session
您需要访问会话的地方