[英]Laravel 5.2 - Session::get not working
I want to display an 'falsh-message' for the user I start to redirect him in the routes.php
with following code : 我想为用户显示“ falsh消息”,并开始使用以下代码在
routes.php
重定向他:
Route::get('/alert',function () {
return redirect()->route('home')->with('message', 'This is a Test Message!');
});
In the alerts.blade.php
that I include in the home view I would like to read this message with: 在我包含在主视图中的
alerts.blade.php
,我想阅读以下消息:
Route::group(['middleware' => ['web']], function () {
Route::get('/alert',function () {
return redirect()->route('home')->with('message', 'This is a Test Message!');
});
});
My Session would be set under storage\\framework\\sessions
with the following code : 我的会话将使用以下代码在
storage\\framework\\sessions
下设置:
a:5:{s:6:"_token";s:40:"8304u3fjR9EhvA88QMVGQnGcgdUcWoZj9LrHaDPh";s:7:"message";s:23:"This is a Test Message!";s:5:"flash";a:2:{s:3:"new";a:0:{}s:3:"old";a:1:{i:0;s:7:"message";}}s:9:"_previous";a:1:{s:3:"url";s:22:"http://localhost/alert";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1452937821;s:1:"c";i:1452937821;s:1:"l";s:1:"0";}}
But the Problem is that Laravel gives me no output from the message because it is empty. 但是问题在于Laravel没有给我任何消息,因为它是空的。 So maybe someone can help me.
所以也许有人可以帮助我。
routes.php
: routes.php
:
Route::group(['middleware' => ['web']], function () {
Route::get('/', [
'uses' => '\App\Http\Controllers\HomeController@index',
'as' => 'home',
]);
Route::get('/alert',function () {
return redirect()->route('home')->with('message', 'This is a Test Message!');
});
});
The web
middleware group contains all the middleware required for sessions. web
中间件组包含会话所需的所有中间件。 Your alert
route is in the middleware group, so it sets the session properly, however your home
route needs to also be in the web
middleware group to be able to read the session. 您的
alert
路由在中间件组中,因此它可以正确设置会话,但是您的home
路由也必须在web
中间件组中才能读取会话。
您可能应该将您的home
路由放入您的route.php文件中的web
中间件中,以使其工作
Insert session flash data explicitly.. 显式插入会话闪存数据。
Route::get('/alert',function () {
$request->session()->flash('message', 'Task was successful!');
return redirect()->route('home');
});
if you write like this : 如果你这样写:
Route::get('/alert',function () {
return redirect()->route('home')->with('message', 'This is a Test Message!');
});
you will find a variable in view named $message; 您将在视图中找到一个名为$ message的变量; try and check
echo $message;
尝试检查
echo $message;
It is one time use; 一次使用; you can also write:
您还可以编写:
Route::get('/alert',function () {
$request->session()->flash('message', 'This is a Test Message!');
return redirect()->route('home');
});
It is also one time use but you need to enable the session. 这也是一次使用,但是您需要启用会话。 If you want the message in session permanently yo can write:
如果您希望会话中的消息永久存在,可以写:
Route::get('/alert',function () {
$request->session()->put('message', 'This is a Test Message!');
return redirect()->route('home');
});
Hope this will helps other to understand more clearly. 希望这将有助于其他人更清楚地了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.