[英]How to redirect the wrong route method to error page in laravel, "The GET method is not supported for this route. Supported methods: POST"
[英]Laravel redirect from controller gives “method is not supported for this route” error
我有一条支持 PUT 的路线:
Route::put('/products/{id}/cancel/', 'ProductController@cancel')->where('id', '[0-9]+');
在 Controller 更新产品的 state 后,它应该重定向到另一条路线:
return redirect('products')->with('success', 'Produto cancelado');
那指向
Route::get('/products', 'ProductController@list')->name('products');
一切正常,直到重定向。 产品已更新,但重定向给出了方法不受支持的错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD
我还尝试了以下重定向:
return redirect('/products/')->with('success', 'Produto cancelado');
和
return redirect()->route('products')->with('success', 'Produto cancelado');
PUT 方法是如何传递的? 我应该在重定向中显式调用 GET 方法吗?
我应该如何解决这个问题?
编辑:我从 HTTPrequest 发送 PUT。 所以我要回到JS function。 我想转发一个接受 with-> 的视图。
EDIT2:我可以返回一个视图,但它只出现在网络>开发人员工具的预览中。
return view('pages.products')->with('success', 'Produto cancelado');
但我显然需要它在 window 中。
我可以在 js 中重定向,但这样我错过了在部分刀片中显示 flash 消息。
我不知道该怎么办。
谢谢
错误不在 GET 方法中。 错误说不支持 put 方法
Route::put('/products/{id}/cancel/', 'ProductController@cancel')->where('id', '[0-9]+');
如果您将其作为domainname.com/products/5/cancel
尝试,则将其定义如下。
Route::get('products/{id}/cancel', 'ProductController@cancel')->where('id', '[0-9]+');
我最终做了一些作弊:
我添加了一条新的 PUT 路线:
Route::put('/products', 'ProductController@list')->name('products_put');
controller 返回:
return redirect()->route('products')->with('success', 'Produto cancelado',200);
并且响应处理程序进行作弊:
document.write(this.responseText);
window.history.pushState('products', 'eBaw · online shopping', '/products');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.