即使使用 Web 中间件,Laravel 5.2 会话闪存也无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36279871/
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 5.2 Session flash not working even with web middleware
提问by howellmartinez
I am trying to implement flash messaging using sessions but am unable to do so.
我正在尝试使用会话实现 Flash 消息传递,但我无法这样做。
In my controller I have:
在我的控制器中,我有:
public function store(Request $request) {
session()->flash('donald', 'duck');
session()->put('mickey', 'mouse');
return redirect()->action('CustomerController@index')->with('bugs', 'bunny');
}
But when I check the session variables in the view, I can only see the values from session()->put('mickey', 'mouse')
.
但是当我检查视图中的会话变量时,我只能看到session()->put('mickey', 'mouse')
.
Session:
会议:
{"_token":"F6DoffOFb17B36eEJQruxvPe0ra1CbyJiaooDn3F","_previous":{"url":"http:\/\/localhost\/customers\/create"},"flash":{"old":[],"new":[]},"mickey":"mouse"}
A lot of people encountered this problem by not having the relevant routes inside the web middleware. I made sure to do this as well but it still wouldn't work.
很多人遇到这个问题是因为 Web 中间件中没有相关的路由。我也确保这样做,但它仍然不起作用。
In routes.php:
在routes.php中:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('/customers', 'CustomerController@index');
Route::get('/customers/create', 'CustomerController@create');
Route::post('/customers', 'CustomerController@store');
});
In Kernel.php:
在 Kernel.php 中:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
Can anyone let me know what I could be doing wrong here? Thanks!
谁能让我知道我在这里做错了什么?谢谢!
回答by howellmartinez
Fixed the issue by replacing
通过替换修复了问题
Route::group(['middleware' => ['web']], function () {
...
});
with
和
Route::group(['middlewareGroups' => ['web']], function () {
...
});
No idea why this works though when all the documentation suggests that we use ['middleware' => ['web']]
尽管所有文档都建议我们使用,但不知道为什么会这样 ['middleware' => ['web']]
回答by ChimeraTheory
This is more than likely because of a change that was made to the Laravel framework (v5.2.27) that all routes by default are part of the "web" middleware, so assigning it again in your routes.php file ends up assigning it twice.
这很可能是因为对 Laravel 框架 (v5.2.27) 所做的更改,默认情况下所有路由都是“web”中间件的一部分,因此在 routes.php 文件中再次分配它最终会分配两次.
The solution is either to remove the "web" middleware from your routes OR remove the automatic assignment from the RouteServiceProvider.
解决方案是从路由中删除“web”中间件,或者从 RouteServiceProvider 中删除自动分配。
Before the Laravel update:
Laravel 更新前:
// RouteServiceProvider.php
$router->group(['namespace' => $this->namespace], function ($router) {
require app_path('Http/routes.php');
});
After the Laravel update:
Laravel 更新后:
// RouteServiceProvider.php
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
Notice how the new update automatically applies the "web" middleware to all routes. Simply remove it here if you wish to continue using Laravel 5.2 as you have before (manually assigning "web" middleware in your routes.php).
请注意新更新如何自动将“web”中间件应用于所有路由。如果你想像以前一样继续使用 Laravel 5.2,只需在此处删除它(在你的 routes.php 中手动分配“web”中间件)。
回答by Chilion
Build your Session flash info by using this code:
使用以下代码构建您的会话闪存信息:
<?php
Session::flash("Donald", "Duck")
// Or in your code style.
$request->session()->flash("Donald", "Duck")
?>
Check it in your view with:
在您的视图中检查它:
@if(Session::has("Donald")
{{Session::get("Donald")}}
@endif
You forget to use $request :)
你忘记使用 $request :)
回答by Sunil
In Controller:
在控制器中:
use Session,Redirect;
public function store(Request $request)
{
Session::flash('donald', 'duck');
Session::put('mickey', 'mouse');
return Redirect::to('/customers')->with('bugs', 'bunny');
}
In 'view' check the data is getting or not:
在“视图”中检查数据是否正在获取:
<?php
print_r($bugs);die;
?>
Good Luck :)
祝你好运 :)
回答by Brotzka
I use the following:
我使用以下内容:
In my controller:
在我的控制器中:
public function xyz(){
// code
// This
return redirect()->action('homeController@index')->with('success', 'Check! Everything done!');
// Or this
return redirect('/index')->with('success', 'Check! Everything done!');
}
In my view:
在我看来:
@if(session('success'))
{{ session('success') }}
@endif
Nothing else. The web-middleware is assigned to every route.
没有其他的。网络中间件被分配给每条路由。
回答by rafwell
I dont know why but on Windows you need changes in your routes: middleware to middlewareGroups, like that:
我不知道为什么,但在 Windows 上,您需要更改路由:中间件到中间件组,如下所示:
So, in your app\Kernel.php, you need put the StartSession at first on array of middleware group web:
因此,在您的 app\Kernel.php 中,您需要首先将 StartSession 放在中间件组 web 数组上: