php Laravel 4 - 重定向回请求来自的同一页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21291395/
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 4 - Redirect back to the same page where the request comes from
提问by Abhay PS
In Laravel 4, I want to redirect the user back to the page from where the request came. For example, a user tries to update his profile so edits the profile and hits SAVE. In controller I do the update and normally I would do Redirect::to('profile')->with('message','Profile saved!'). But what I want is to simply redirect it back with message. May be something like Redirect::back()->with('message','Operation Successful !')if this is available. I want it as it is more generic and I can use it anywhere.
在 Laravel 4 中,我想将用户重定向回请求来自的页面。例如,用户尝试更新他的个人资料,因此编辑个人资料并点击“保存”。在控制器中,我进行更新,通常我会这样做Redirect::to('profile')->with('message','Profile saved!')。但我想要的是简单地将它重定向回消息。可能是这样的,Redirect::back()->with('message','Operation Successful !')如果这可用。我想要它,因为它更通用,我可以在任何地方使用它。
回答by Antonio Carlos Ribeiro
Yes this is available:
是的,这是可用的:
return Redirect::back()->with('message','Operation Successful !');
But since this is a redirected request, you have to access the message by using:
但由于这是一个重定向请求,您必须使用以下方法访问该消息:
echo Session::get('message');
回答by Erik Aybar
In Short: Yes
简而言之:是的
You can certainly use
你当然可以使用
Redirect::back()->withMessage('Profile saved!')
Redirect::back()->withMessage('Profile saved!')
in place of
代替
Redirect::to('profile')->withMessage('Profile saved!')
Redirect::to('profile')->withMessage('Profile saved!')
*nifty feature in Laravel that it parses your camelCase on the ->with('name', 'value')so that ->withName('value')works just the same.
* Laravel 中的一个很好的功能,它可以解析你的驼峰命名法,->with('name', 'value')所以->withName('value')工作原理是一样的。
Also....
还....
I'm assuming your form is bound to the model such as Form::model($user, [...]to pre-fill form fields, but if not you may want to re-flash the input on the Redirect (or if your validation failed and you want to user to be able to correct the invalid info).
我假设您的表单绑定到模型,例如Form::model($user, [...]预填充表单字段,但如果不是,您可能希望重新刷新重定向上的输入(或者如果您的验证失败并且您希望用户能够更正无效信息)。
Just a snippet [untested]...
只是一个片段[未经测试]...
// [[... validation and other magic here]]
if ($validator->fails()) {
return Redirect::back()
->withMessage($message_fail)
->withErrors($validator)
->withInput();
}
return Redirect::back()
->withMessage($message_success)
Hope that helps!
希望有帮助!
Twitter: @ErikOnTheWeb
推特:@ErikOnTheWeb
回答by chris342423
You should consider not to use Redirect::back(). Yes, it's tempting and seemsto be exactly what you need. But:
您应该考虑不使用Redirect::back(). 是的,这很诱人,而且似乎正是您所需要的。但:
The back()method uses the "referer" attribute of the request header. So the user agent, usually a browser, tells the server (and Laravel) the URL he comes from. (as Wikipedia says: referer is a misspelling of referrer) But not every user agent / browser will provide this information!I use Opera and I do not allow it to transmit the referer in generally! So back()won't work for me. (Yes, I can allow this for a site but I'm way to lazy. And sorry, I don't trust your site.)
该back()方法使用请求标头的“referer”属性。所以用户代理,通常是浏览器,告诉服务器(和 Laravel)他来自的 URL。(正如维基百科所说:referer 是referrer 的拼写错误)但并非每个用户代理/浏览器都会提供此信息!我使用Opera,一般不允许它传输referer!所以back()不会为我工作。(是的,我可以在网站上允许这样做,但我太懒了。对不起,我不信任你的网站。)

