如何重定向到 Laravel 中的 POST 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45443085/
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
How to redirect to a POST route in Laravel
提问by Muhammad
In my controller, I have a method which should redirect to a POST
route. I can not make this route a GET
one either. Is there any solutions?
在我的控制器中,我有一个应该重定向到POST
路由的方法。我也不能使这条路线成为GET
一条路线。有什么解决办法吗?
My controller code looks like:
我的控制器代码如下所示:
// After a bunch of other if statements, etc
return redirect('newUser')->with('user', $user);
Now my route (in web.php) is as follows:
现在我的路线(在 web.php 中)如下:
Route::post('/newUser', function() {
$user=session('user');
dd($user);
return view('profileNewUser', $user);
});
The error which is thrown at me is MethodNotAllowedHTTPException
.
向我抛出的错误是MethodNotAllowedHTTPException
.
I know my error is something to do with the _token
, is there anyway I can allow the token field to get passed on? I tried passing it in with the with
in my redirect but that doesn't work either.
我知道我的错误与 有关_token
,无论如何我可以允许传递令牌字段吗?我尝试with
在我的重定向中将其传入,但这也不起作用。
Thanks for your help.
谢谢你的帮助。
回答by CoursesWeb
Try redirect to Named Route.
尝试重定向到命名路由。
Route::post('/newUser', function() {
$user=session('user');
dd($user);
return view('profileNewUser', $user);
})->name('newusr');
Then:
然后:
return redirect()->route('newusr', ['user'=>$user]);
Or, redirect to controller actions.
或者,重定向到控制器操作。
- See this tutorial about redirects: http://coursesweb.net/laravel/redirects
- 请参阅有关重定向的本教程:http: //coursesweb.net/laravel/redirects
回答by Oluwatobi Samuel Omisakin
Since you need the url to be 'newUser' then you can simply make a GET
route of the same url.
由于您需要 url 为“newUser”,因此您可以简单地创建GET
相同 url的路由。
The question as to Why you would want to return the post request to the same routeis still valid, because it might lead to infinite call of the same route.
关于为什么要将 post 请求返回到同一路由的问题仍然有效,因为它可能导致对同一路由的无限调用。
One way however, is you can add a logic to check if user is already in session then do otherwise, else this kind of pattern is not really good.
但是,一种方法是您可以添加一个逻辑来检查用户是否已经在会话中,然后再执行其他操作,否则这种模式并不是很好。
Best way is to separate your page that returns the response of your POST from the one that renders a view.
最好的方法是将返回 POST 响应的页面与呈现视图的页面分开。
Like I suggested in the comment: you can simply move this getUser
route into a controller then use Laravel's Response to action or the other way. Here is the answerin case you want to browse.
就像我在评论中建议的那样:您可以简单地将此getUser
路线移动到控制器中,然后使用 Laravel 的动作响应或其他方式。如果您想浏览,这里是答案。
PS: my answer is based on my understanding of your question. Thank you.
PS:我的回答是基于我对你的问题的理解。谢谢你。
回答by omarjebari
As far as i can see you can only redirect to a post route by using:
据我所知,您只能使用以下命令重定向到 post 路由:
return back()->withInput(['postvar1' => 'postval1']); // L5.5
This is used under the circumstance when there is an error in a form request.
这在表单请求中存在错误的情况下使用。