Laravel 重定向::withInput()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20831530/
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 Redirect::withInput()
提问by M K
As the title says - I'm trying to redirect back to previous page, with input data, like this:
正如标题所说 - 我正在尝试使用输入数据重定向回上一页,如下所示:
return Redirect::back()->withInput();
It works as intended for regular input, but not for files! Is there a workaround for this? So that after the redirect, the previous file is selected again.
它适用于常规输入,但不适用于文件!有解决方法吗?以便在重定向后,再次选择上一个文件。
采纳答案by Antonio Carlos Ribeiro
It won't work.
它不会工作。
When you redirect something in Laravel it stores $_POST and $_GET in Session to get you data back in the next request. Files comes in a special PHP global var, $_FILES, because they are not really in memory, they are in disk and just some info about them in memory.
当您在 Laravel 中重定向某些内容时,它会将 $_POST 和 $_GET 存储在 Session 中,以便在下一个请求中取回数据。文件包含在一个特殊的 PHP 全局变量 $_FILES 中,因为它们实际上并不在内存中,而是在磁盘中,而在内存中只是关于它们的一些信息。
Storing those files in Session could cost too much in resources, imagine storing them in the Session you store in database... Yeah, Laravel or Symfony could create a layer to deal with it, looks easy at first sight, but looks like they just decided not to.
将这些文件存储在 Session 中可能会消耗太多资源,想象一下将它们存储在你存储在数据库中的 Session 中......是的,Laravel 或 Symfony 可以创建一个层来处理它,乍一看很容易,但看起来他们只是决定不这样做。
So, IMO, if you need them in the next request, move them to a temporary area and Session::put() the info about them, so you can just Session::get() them in the next request.
因此,IMO,如果您在下一个请求中需要它们,请将它们移动到一个临时区域,然后 Session::put() 获取有关它们的信息,这样您就可以在下一个请求中使用 Session::get() 它们。
回答by M K
As mentioned here, there is no straight forward way to do this. A valuable solution might be, saving the file somewhere, upon the upload, and then populating your form, after the redirect, with an additional input field, that contains the information about your previously uploaded file. That way you'll be able to decide on the server side, wether to take the old one (in case there wasn't a new file uploaded) or the new one.
正如这里提到的,没有直接的方法可以做到这一点。一个有价值的解决方案可能是,在上传时将文件保存在某处,然后在重定向后填充您的表单,并添加一个额外的输入字段,其中包含有关您之前上传的文件的信息。这样你就可以决定在服务器端,是采用旧的(以防没有上传新文件)还是新的。
回答by elliotanderson
This may work:
这可能有效:
return Redirect::back()->with('file', Input::file('file_name');
回答by Spycomb
If you make with form like
如果你用这样的形式制作
Form::text("name",null);
it would be work, try it :)
它会起作用,试试吧:)
回答by Naveen Gamage
I would go for a java-script approach, just validate input data in browser using java-script if input is good, let them submit the form. just an suggestion..!!
我会采用 java-script 方法,如果输入良好,只需使用 java-script 验证浏览器中的输入数据,让他们提交表单。只是一个建议..!!