php 此路由不支持 POST 方法。支持的方法:GET、HEAD。Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55178647/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:33:53  来源:igfitidea点击:

The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel

phplaravel

提问by JohnSmith2521

I have a problem with my edit page. When I submit I get this error:

我的编辑页面有问题。当我提交时,我收到此错误:

The POST method is not supported for this route. Supported methods: GET, HEAD.

此路由不支持 POST 方法。支持的方法:GET、HEAD。

I have no clue where it comes from as I am pretty new to Laravel.

我不知道它来自哪里,因为我对 Laravel 还很陌生。

routes(web.php):

路线(web.php):

Route::group(['middleware' => 'auth'], function () {
Route::get('/', 'ProjectController@index');

Route::get('/projects/{id}', 'ProjectController@show');
Route::post('/create','ProjectController@store');
Route::get('/create', 'ProjectController@create');
Route::get('/projects/{id}/delete', 'ProjectController@destroy');
Route::put('/edit','ProjectController@update');
Route::get('/projects/{id}/edit', 'ProjectController@edit');

});

Controller:

控制器:

 public function edit($id)
    {
        return view('project.edit',[

            'project' => Project::find($id)
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $project = Project::find($request->id);
        $project->project_name = $request->input('project_name');
        $project->client = $request->input('client');
        $project->description = $request->input('description');
        $project->time_span = $request->input('time_span');
        $project->text_report = $request->input('text_report');
        $project->created_by = $request->input('created_by');

        $project->save();

        return  redirect('/')->with('success', 'Project aangepast');
    }

enter image description here

在此处输入图片说明

采纳答案by Khaldoun Nd

There are multiple ways you can handle this:

您可以通过多种方式处理此问题:

  1. If you insist on using PUTyou can change the form action to POSTand add a hidden method_fieldthat has a value PUTand a hidden csrf field (if you are using blade then you just need to add @csrf_fieldand {{ method_field('PUT') }}). This way the form would accept the request.

  2. You can simply change the route and form method to POST. It will work just fine since you are the one defining the route and not using the resource group.

  1. 如果您坚持使用,PUT您可以将表单操作更改为POST并添加一个隐藏的method_field具有值PUT和隐藏的 csrf 字段(如果您使用刀片,那么您只需要添加@csrf_field{{ method_field('PUT') }})。这样表单就会接受请求。

  2. 您可以简单地将路由和表单方法更改为POST. 它会正常工作,因为您是定义路由而不是使用资源组的人。

回答by stoi2m1

I know this is not the solution to OPs post. However, this post is the first one indexed by Google when I searched for answers to this error. For this reason I feel this will benefit others.

我知道这不是 OP 帖子的解决方案。但是,当我搜索此错误的答案时,这篇文章是 Google 索引的第一篇文章。出于这个原因,我觉得这将使其他人受益。

The following error...

以下错误...

The POST method is not supported for this route. Supported methods: GET, HEAD.

此路由不支持 POST 方法。支持的方法:GET、HEAD。

was caused by not clearing the routing cache

是因为没有清除路由缓存

php artisan route:cache

回答by stoi2m1

I've seen your code in web.php as follows: Route::post('/edit/{id}','ProjectController@update');

我在 web.php 中看到你的代码如下:Route::post('/edit/{id}','ProjectController@update');

Step 1: remove the {id} random parameter so it would be like this: Route::post('/edit','ProjectController@update');

第 1 步:删除 {id} 随机参数,使其如下所示:Route::post('/edit','ProjectController@update');

Step 2: Then remove the @method('PUT') in your form, so let's say we'll just plainly use the POSTmethod

第 2 步:然后删除表单中的 @method('PUT'),假设我们只是简单地使用POST方法

Then how can I pass the ID to my method?

那么如何将 ID 传递给我的方法呢?

Step 1: make an input field in your form with the hidden attribute for example

第 1 步:例如在您的表单中使用 hidden 属性创建一个输入字段

<input type="hidden" value="{{$project->id}}" name="id">

<input type="hidden" value="{{$project->id}}" name="id">

Step 2: in your updatemethod in your controller, fetch that ID for example:

第 2 步:在控制器的更新方法中,获取该 ID,例如:

$id = $request->input('id');

then you may not use it to find which project to edit

那么您可能无法使用它来查找要编辑的项目

$project = Project::find($id)
//OR
$project = Project::where('id',$id);

回答by pezhman vaziri

add @method('PUT') on the form

在表单上添加@method('PUT')

exp:

经验:

<form action="..." method="POST">

@csrf  

@method('PUT')



</form>

回答by Paulo Cardozo

If you are using a Route::group, with a vendor plugin like LaravelLocalization (from MCAMARA), you need to put POST routes outside of this group. I've experienced problems with POST routes using this plugin and I did solved right now by putting these routes outside Route::group..

如果您使用 Route::group 和 LaravelLocalization(来自 MCAMARA)等供应商插件,则需要将 POST 路由放在该组之外。我使用这个插件遇到了 POST 路由的问题,我现在通过将这些路由放在 Route::group 之外解决了。

回答by Harry Bosh

I just removed the slash at the end of url and it began working... /managers/games/id/push/to:

我刚刚删除了 url 末尾的斜杠,它开始工作...... /managers/games/id/push/到:

$http({
  method: 'POST',
  url: "/managers/games/id/push",

This may have to do with upgrading to laravel 5.8?

这可能与升级到 Laravel 5.8 有关?

回答by Shane Kelly

If you have a seeder in your database, run php artisan migrate:fresh --seed

如果您的数据库中有播种机,请运行 php artisan migrate:fresh --seed

回答by Anoop P S

Hi you dont have to write all the routes just follow the conventions https://laravel.com/docs/5.8/controllerscheck : Actions Handled By Resource Controllersection

嗨,您不必编写所有路由,只需遵循约定 https://laravel.com/docs/5.8/controllers检查:资源控制器处理的操作部分

Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method. When posting a data from n laravel you have to use,

由于 HTML 表单无法发出 PUT、PATCH 或 DELETE 请求,因此您需要添加一个隐藏的 _method。从 n laravel 发布数据时,您必须使用,

<form action="/foo/bar" method="POST">
@method('PUT')
</form>

回答by Vinit Dabhi

mainly this type of error generate, 1.first check a code, in code, we define @csrf

主要是这种类型的错误产生,1.首先检查一段代码,在代码中,我们定义@csrf

<form method ="post" action={{url('project'')}}
   @csrf
   ...... 

2.when we define a wrong variable name, that time also happened this type of problem.

2.当我们定义了一个错误的变量名时,那个时候也发生了这种类型的问题。

ex. if your database field name "xyz" and you use a "wxyz"

前任。如果您的数据库字段名称为“xyz”并且您使用“wxyz”

3.if our method is wrong in form,so plz check our method. ex. <form method="post">

3.如果我们的方法形式错误,请检查我们的方法。前任。<form method="post">

回答by idrissAbbou

The easy way to fix this is to add this to your form.

解决此问题的简单方法是将其添加到您的表单中。

{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">

then the update method will be like this :

那么更新方法将是这样的:

public function update(Request $request, $id)
{
    $project = Project::findOrFail($id);
    $project->name = $request->name;
    $project->description = $request->description;

    $post->save();
}