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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:06:15  来源:igfitidea点击:

Laravel 5.2.* redirect->back->with doesnt work

phplaravelredirectparameters

提问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 Sessionmessage in all your views is not being utilized because it was moved from the global middleware to the webmiddleware group.

这是 5.2 升级的一个重大问题。发生的事情是负责Session在所有视图中显示消息的中间件未被使用,因为它已从全局中间件移至web中间件组。

There are two ways to fix this:

有两种方法可以解决这个问题:

  1. In your kernel.phpfile(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::classback to the protected $middlewareproperty.
  2. Wrap all your webroutes 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) 
    });
    
  1. 在您的kernel.php文件(app/Http/Kernel.php)中,您可以将其middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class移回protected $middleware属性。
  2. 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()将带您到带有保存“数据库错误!”的数据变量的上一页