Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel返回的MethodNotAllowedHttpException路由不存在_Php_Laravel_Post_Get_Routing - Fatal编程技术网

Php Laravel返回的MethodNotAllowedHttpException路由不存在

Php Laravel返回的MethodNotAllowedHttpException路由不存在,php,laravel,post,get,routing,Php,Laravel,Post,Get,Routing,我在laravel 5.7中创建了一条购买物品的路线 Route::post('/buy/item', "userController@buy_item")->name('buy_item_form'); 一切正常,但当我刷新页面(替换以获取请求)时,我得到一个 方法不允许HttpException。 GET路由不存在,它必须返回404错误。 我不明白为什么它会返回我这个例外。您使用的是一个post,在post中您有一个@csrf令牌。当您单击refresh时,您正在执行一个GET方法而

我在laravel 5.7中创建了一条购买物品的路线

Route::post('/buy/item', "userController@buy_item")->name('buy_item_form');
一切正常,但当我刷新页面(替换以获取请求)时,我得到一个 方法不允许HttpException。 GET路由不存在,它必须返回404错误。
我不明白为什么它会返回我这个例外。

您使用的是一个post,在post中您有一个@csrf令牌。当您单击refresh时,您正在执行一个GET方法而不是post,因此您可以获取方法notallowexception。如果不发送数据,可以将其更改为get[Route::get]方法

如果您想要接受这两种方法[post,get],以获得更好的体验并管理可能的错误。您可以接受路由上的两种方法,如:

Route::match(array('GET','POST'),'/buy/item', 'userController@buy_item')->name('buy_item_form');
在控制器上,根据方法定义要执行的操作

if (Request::isMethod('get')){
    // redirect user
}

if (Request::isMethod('post')){
    // do logic for post method
}


您使用的是post,post中有@csrf令牌。当您单击refresh时,您正在执行一个GET方法而不是post,因此您可以获取方法notallowexception。如果不发送数据,可以将其更改为get[Route::get]方法

如果您想要接受这两种方法[post,get],以获得更好的体验并管理可能的错误。您可以接受路由上的两种方法,如:

Route::match(array('GET','POST'),'/buy/item', 'userController@buy_item')->name('buy_item_form');
在控制器上,根据方法定义要执行的操作

if (Request::isMethod('get')){
    // redirect user
}

if (Request::isMethod('post')){
    // do logic for post method
}


我没有创建GET路线。因此,如果路由不存在,为什么我在GET请求时遇到异常。这将返回404错误。当您刷新或转到一个页面时,如果您提交数据并将表单上的方法设置为post,它将发布信息而不是get。我的意思是,我创建了一个Post路由,当我在浏览器的url地址栏上写入Post路由的url时(发出get请求),它返回一个异常,而不是404错误。我可以创建一个get-route thet-do-abort(404),但无论如何都不一定要发生?将此添加到代码中,您将看到您正在执行一个get-on-refresh-route::get('/buy/item',function(){return“get-Request”;});我可以这样做:Route::get('/buy/item',function(){return abort(404)});但我的问题是为什么不是自动发生的。我没有创建一个GET路径。因此,如果路由不存在,为什么我在GET请求时遇到异常。这将返回404错误。当您刷新或转到一个页面时,如果您提交数据并将表单上的方法设置为post,它将发布信息而不是get。我的意思是,我创建了一个Post路由,当我在浏览器的url地址栏上写入Post路由的url时(发出get请求),它返回一个异常,而不是404错误。我可以创建一个get-route thet-do-abort(404),但无论如何都不一定要发生?将此添加到代码中,您将看到您正在执行一个get-on-refresh-route::get('/buy/item',function(){return“get-Request”;});我可以这样做:Route::get('/buy/item',function(){return abort(404)});但我的问题是为什么不是自动发生的。