php Laravel: MethodNotAllowedHttpException: 此路由不支持 GET 方法。支持的方法:POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55219001/
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: MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST
提问by Joe
I have a very weird problem. I have a post route but I receive an error that The GET method is not supported for this route.
我有一个非常奇怪的问题。我有一条后路由,但收到一条错误消息,指出这条路由不支持 GET 方法。
This is my web.php function:
这是我的 web.php 函数:
Route::post('/sender',function () {
$text = request()->text;
event(new FormSubmitted($text));
});
I'm definitely sending a post request. I've already checked this: Laravel: POST method returns MethodNotAllowedHttpException
我肯定会发送一个post请求。我已经检查过这个:Laravel: POST 方法返回 MethodNotAllowedHttpException
But the chosen answer is unclear.
但选择的答案尚不清楚。
My View Code:
我的查看代码:
<?php echo csrf_field(); ?>
{{ csrf_field() }}
<form action="/sender" method="post>
First name: <input type="text" name="fname"><br>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="content"><br>
<input type="submit">
回答by CodeBoyCode
I believe that this might just be a typo error - you have missed a quotation mark (") after 'post'
我相信这可能只是一个错字错误 - 您在“发布”之后错过了引号(“)
view:
看法:
<form action="/sender" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
First name: <input type="text" name="fname"><br>
<input type="text" name="content"><br>
<input type="submit">
</form>
controller
控制器
Route::post('/sender',function () {
$name = request->fname;
$content = request->content
event(new FormSubmitted($name, $content));
});
EDIT: updated controller code, you were requesting the data from an input called 'text', but there wasn't any inputs with the name of 'text' in the view, only input type's
编辑:更新了控制器代码,您从名为“text”的输入请求数据,但视图中没有任何名称为“text”的输入,只有输入类型
回答by bhavinjr
First, check you define route proper or not by php artisan route:list
command
首先,通过php artisan route:list
命令检查您是否正确定义了路由
Blade file
刀片文件
<form action="{{ route('sender') }}" method="post">
@csrf
First name: <input type="text" name="fname"><br>
<input type="text" name="content"><br>
<input type="submit">
Route
路线
Route::post('/sender',function () {
$text = request()->fname; //access by input field name
event(new FormSubmitted($text));
})->name('sender');
or
Route::post('/sender', 'UserController@sender')->name('sender');
if you're using route with controller then your controller seems like that
如果您使用带有控制器的路由,那么您的控制器看起来就是这样
public function sender(Request $request)
{
$fname = $request->fname;
event(new FormSubmitted($fname));
}