Laravel.重定向旨在发布方法 [英] Laravel. Redirect intended to post method

查看:87
本文介绍了Laravel.重定向旨在发布方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5,这是我的问题.用户填写表格X,如果他未登录,则他将被重定向以填写更多字段表格,或者他可以登录.如果用户填写其他字段,一切正常,但是如果他登录,则laravel重定向用户使用GET方法而不是POST来形成X.

I'm using laravel 5 and this is my problem. User fill in form X and if he isin't logged in, he gets redirected to fill in more fields form OR he gets possibility to log in. Everything works just fine, if user fill in additional fields, but if he login, laravel redirects user to form X with GET method instead of POST.

这是我的中间件重定向的样子:

This is how my middleware redirect looks like:

return redirect()->guest('user/additional-fields');

此重定向将在成功登录时出现:

This redirect appears on successfull log in:

return redirect()->intended();

所以在重定向时我得到了错误 MethodNotAllowedHttpException. URL是正确的,它被定义为POST方法.我在这里想念什么? laravel为什么将重定向重定向为GET方法?我该如何解决这个问题?谢谢!

So on redirect intended i get error MethodNotAllowedHttpException. URL is correct which is defined as POST method. What am I missing here? Why does laravel redirects intended as GET method? How could I solve this problem? Thanks!





Route::post('/user/log-in-post', ['as' => 'user-log-in-post', 'uses' => 'UserController@postUserLogIn']);

这是我的路线,希望这是您需要的路线.

This is my route, I hope this is one you need.

推荐答案

您可以使用命名路由来解决此问题:

You can use a named route to solve this issue:

让我们这样命名路由:

获取

Route::get('user/additional-fields',array(
    'uses' => 'UserController@getAdditionalFields',
    'as'   => 'user.getAdditionalFields'
));

发帖

Route::post('user/additional-fields',array(
    'uses' => 'UserController@postAdditionalFields',
    'as'   => 'user.postAdditionalFields'
));

因此,我们现在可以通过这样做确保Laravel使用正确的路线

So we can now ensure Laravel uses the right route by doing this

return redirect()->guest(route('user.getAdditionalFields'));

还请注意,由于Laravel希望提交表单,因此无法重定向POST.所以你不能这样做:

Also note that its not possible to redirect a POST because Laravel expects form to be submitted. SO you can't do this:

return redirect()->guest(route('user.postAdditionalFields'));

除了您使用cURL或GuzzleHttp之类的东西来模拟发布请求

except you use something like cURL or GuzzleHttp simulate a post request

这篇关于Laravel.重定向旨在发布方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆