Laravel 5.6.14 中的“405 方法不允许”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49569707/
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
"405 Method not allowed" in Laravel 5.6.14
提问by aslamdoctor
I am just learning laravel resource methods to build a basic API. Below is the code of my api.phpfile that shows all the API routes.
我只是在学习 laravel 资源方法来构建基本的 API。下面是我的api.php文件的代码,它显示了所有 API 路由。
// List Articles
Route::get('articles', 'ArticleController@index');
// List Single Article
Route::get('article/{id}', 'ArticleController@show');
// Create New Article
Route::post('article', 'ArticleController@store');
// Update Article
Route::put('article', 'ArticleController@store');
// Delete Article
Route::delete('article/{id}', 'ArticleController@destroy');
This works perfectly on getand deletemethods. But for Post method, it is throwing error "405 Method not allowed". I am using Postman to test the API calls.
这适用于get和delete方法。但是对于 Post 方法,它会抛出错误“405 Method not allowed”。我正在使用 Postman 来测试 API 调用。
To be specific, below is the exact error Postman shows
具体来说,以下是邮递员显示的确切错误
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
采纳答案by Kostiantyn Shtukert
回答by Robert
A MethodNotAllowedHttpException
indicates the POST route can not be found for the requested url, but other methods are available.
AMethodNotAllowedHttpException
表示无法找到请求的 url 的 POST 路由,但可以使用其他方法。
This can be because you did not define it (correctly), or it has a conflict with another route in your config.
这可能是因为您没有(正确)定义它,或者它与您的配置中的另一个路由冲突。
You can check the current routes with php artisan route:list
您可以使用以下命令检查当前路线 php artisan route:list
If you want to use resource controllers, instead of defining all the resource routes and actions yourself, why are you not using the Route::resource()
method?
如果要使用资源控制器,而不是自己定义所有资源路由和操作,为什么不使用该Route::resource()
方法?
Route::resource('article', ArticleController::class);
This will generate all resource routes for you:
这将为您生成所有资源路由:
Verb Path Action Route Name
GET /article index article.index
GET /article/create create article.create
POST /article store article.store
GET /article/{article} show article.show
GET /article/{article}/edit edit article.edit
PUT/PATCH /article/{article} update article.update
DELETE /article/{article} destroy article.destroy
The action translates to the action name in your controller, so for example, a request to POST /article
will call the controller action: ArticleController@store
.
该操作转换为您控制器中的操作名称,例如,请求POST /article
将调用控制器操作:ArticleController@store
。
In your case, I see that you are not using create or edit views, so instead of using the Route::resource()
method, you can use the Route::apiResource()
method, which will exclude routes that present HTML views for creating and editing your articles.
在您的情况下,我看到您没有使用创建或编辑视图,因此Route::resource()
您可以使用该Route::apiResource()
方法代替使用该方法,该方法将排除呈现 HTML 视图以创建和编辑您的文章的路由。
Route::apiResource('article', Api\ArticleController::class);
This will create your routes like:
这将创建您的路线,如:
Verb Path Action Route Name
GET /article index article.index
POST /article store article.store
GET /article/{article} show article.show
PUT/PATCH /article/{article} update article.update
DELETE /article/{article} destroy article.destroy
You can also auto-generate the resource controller to match your resource routes, this will generate the controller file for you.
您还可以自动生成资源控制器以匹配您的资源路由,这将为您生成控制器文件。
php artisan make:controller Api/ArticleController --api
This will generate that file in Http/Controllers/Api/ArticleController
with a mock of all the actions defined by the route which you can then use.
这将Http/Controllers/Api/ArticleController
使用由路由定义的所有操作的模拟生成该文件,然后您可以使用该文件。
More info on resource controllers
PS.
附注。
Your PUT route does not take an id and it calls store, it is good practice to split the actions for POST (creating new) and PUT/PATCH (full/partial update of existing objects) in your controller.
您的 PUT 路由不接受 id 并且它调用 store,在您的控制器中拆分 POST(创建新对象)和 PUT/PATCH(现有对象的全部/部分更新)的操作是一种很好的做法。
Reason for this is that by convention, POST will create a new entity and doing a post again will (most likely) create another, so every request will have a different result.
这样做的原因是按照惯例,POST 将创建一个新实体,再次发布将(很可能)创建另一个,因此每个请求都会有不同的结果。
PUT requests, on the other hand, are idempotent, meaning you should be able to do a PUT request multiple times on the same object and the output should be the same for all these requests. PATCH is a bit of a weird one here, it can be idempotent, but is not required. But when using Laravel, PATCH requests are usually handled by the same controller action which handles the PUT requests, and (depending on implementation) will be idempotent.
另一方面,PUT 请求是幂等的,这意味着您应该能够对同一个对象多次执行 PUT 请求,并且所有这些请求的输出应该相同。PATCH 在这里有点奇怪,它可以是幂等的,但不是必需的。但是在使用 Laravel 时,PATCH 请求通常由处理 PUT 请求的相同控制器操作处理,并且(取决于实现)将是幂等的。
PSS.
PSS。
I would not recommend using POST /article/store
and follow the REST convention of doing a POST on the resource name itself instead. POST /article
我不建议使用POST /article/store
和遵循对资源名称本身执行 POST 的 REST 约定。POST /article
回答by Adam Kozlowski
The methodNotAllowed
exception indicates that a route doesn't exist for the HTTP method you are requesting.
该methodNotAllowed
异常表明您请求的 HTTP 方法不存在路由。
Your form is set up to make a POST
request, so your route needs to use Route::post()
to receive this.
您的表单已设置为发出POST
请求,因此需要使用您的路由Route::post()
来接收此请求。
Be sure that request on postmanis set on POST
确保对邮递员的请求设置为POST
Remember to clear route cache after any route change:
记住在任何路由更改后清除路由缓存:
php artisan route:cache
Try also changing settings on postman: Do not send anything in headers - I mean delete Content-Type
还尝试更改邮递员的设置:不要在标题中发送任何内容 - 我的意思是删除 Content-Type