Laravel 5.2.* 重定向-> 返回-> with 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35001261/
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.* redirect->back->with doesnt work
提问by Phil795
I want to write some data to my database, if the query fails I want to display an error. I tried the following:
我想将一些数据写入我的数据库,如果查询失败我想显示一个错误。我尝试了以下方法:
return redirect()->back()->with('data', ['Database Error!']);
The redirect works great, but I cant read out the response in $data
重定向效果很好,但我无法读出 $data 中的响应
My Blade:
我的刀片:
@if (session()->has('data'))
<div class="alert alert-danger" role="alert">...</div>
@endif
//Second try
<?php if(session('data')) echo $message; ?>
But both methods doesn't work, i read a lot of L5.2 Docs but nothing works.
但是这两种方法都不起作用,我阅读了很多 L5.2 文档但没有任何效果。
Do i need to modify the session config?!? Or what is the problem?
我需要修改会话配置吗?!?或者是什么问题?
采纳答案by smartrahat
This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for display the Session
message in all your views is not being utilized because it was moved from the global middleware to the web
middleware group.
这是 5.2 升级的一个重大问题。发生的事情是负责Session
在所有视图中显示消息的中间件未被使用,因为它已从全局中间件移至web
中间件组。
There are two ways to fix this:
有两种方法可以解决这个问题:
- In your
kernel.php
file(app/Http/Kernel.php), you can move themiddleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
back to theprotected $middleware
property. Wrap all your
web
routes with a route group and apply the web middleware to them:Route::group(['middleware' => 'web'], function() { // Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here) });
- 在您的
kernel.php
文件(app/Http/Kernel.php)中,您可以将其middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
移回protected $middleware
属性。 web
用一个路由组包裹你的所有路由,并将 web 中间件应用到它们:Route::group(['middleware' => 'web'], function() { // Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here) });
回答by Oddadmix
try
return back()->with('data', ['Database Error!']);
尝试
return back()->with('data', ['Database Error!']);
back()
will take you to the previous page with a data variable that holds 'Database Error!'
back()
将带您到带有保存“数据库错误!”的数据变量的上一页