Laravel 5.6.14中的“ 405方法不允许”

asl*_*tor 4 php laravel laravel-5

我只是在学习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');
Run Code Online (Sandbox Code Playgroud)

这在getdelete方法上非常有效。但是对于Post方法,它会引发错误“ 405方法不允许”。我正在使用Postman测试API调用。

具体来说,以下是Postman显示的确切错误

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Run Code Online (Sandbox Code Playgroud)

还附有邮递员的屏幕截图 在此处输入图片说明

Rob*_*ert 7

AMethodNotAllowedHttpException表示无法找到请求的 url 的 POST 路由,但可以使用其他方法。

这可能是因为您没有(正确)定义它,或者它与您的配置中的另一个路由冲突。

您可以使用以下命令检查当前路线 php artisan route:list

如果要使用资源控制器,而不是自己定义所有资源路由和操作,为什么不使用该Route::resource()方法?

Route::resource('article', ArticleController::class);
Run Code Online (Sandbox Code Playgroud)

这将为您生成所有资源路由:

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
Run Code Online (Sandbox Code Playgroud)

该操作转换为您控制器中的操作名称,例如,请求POST /article将调用控制器操作:ArticleController@store

在您的情况下,我看到您没有使用创建或编辑视图,因此Route::resource()您可以使用该Route::apiResource()方法而不是使用该方法,该方法将排除呈现 HTML 视图以创建和编辑您的文章的路由。

Route::apiResource('article', Api\ArticleController::class);
Run Code Online (Sandbox Code Playgroud)

这将创建您的路线,如:

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
Run Code Online (Sandbox Code Playgroud)

您还可以自动生成资源控制器以匹配您的资源路由,这将为您生成控制器文件。

php artisan make:controller Api/ArticleController --api
Run Code Online (Sandbox Code Playgroud)

这将生成该文件,Http/Controllers/Api/ArticleController其中包含路由定义的所有操作的模拟,然后您可以使用该文件。

有关资源控制器的更多信息

附注。

您的 PUT 路由不接受 id 并且它调用 store,最好在控制器中拆分 POST(创建新对象)和 PUT/PATCH(现有对象的全部/部分更新)的操作。

这样做的原因是按照惯例,POST 将创建一个新实体,再次发布将(很可能)创建另一个,因此每个请求都会有不同的结果。

另一方面,PUT 请求是幂等的,这意味着您应该能够对同一个对象多次执行 PUT 请求,并且所有这些请求的输出应该相同。PATCH 在这里有点奇怪,它可以是幂等的,但不是必需的。但是在使用 Laravel 时,PATCH 请求通常由处理 PUT 请求的相同控制器操作处理,并且(取决于实现)将是幂等的。

PSS。

我不建议使用POST /article/store并遵循对资源名称本身执行 POST 的 REST 约定。POST /article


Kos*_*ert 5

像这样更改您的存储路线:

Route::post('article/store', 'ArticleController@store');
Run Code Online (Sandbox Code Playgroud)

因为您将邮递员的邮寄请求发送到

/ article / store

在此处输入图片说明