簡體   English   中英

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

[英]“405 Method not allowed” in Laravel 5.6.14

我只是在學習 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');

這適用於getdelete方法。 但是對於 Post 方法,它會拋出錯誤“405 Method not allowed”。 我正在使用 Postman 來測試 API 調用。

具體來說,以下是郵遞員顯示的確切錯誤

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

還附上郵遞員的截圖在此處輸入圖片說明

像這樣改變你的存儲路線:

Route::post('article/store', 'ArticleController@store');

因為您從 Postman 發送 post 請求到

/文章/商店

在此處輸入圖片說明

MethodNotAllowedHttpException表示無法為請求的 url 找到 POST 路由,但其他方法可用。

這可能是因為您沒有(正確)定義它,或者它與您的配置中的另一個路由沖突。

您可以使用php artisan route:list檢查當前路線

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

Route::resource('article', ArticleController::class);

這將為您生成所有資源路由:

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

動作轉換為控制器中的動作名稱,例如,對POST /article的請求將調用控制器動作: ArticleController@store

在您的情況下,我看到您沒有使用創建或編輯視圖,因此您可以使用Route::apiResource()方法而不是使用Route::resource()方法,該方法將排除呈現 HTML 視圖的路由創建和編輯您的文章。

Route::apiResource('article', Api\ArticleController::class);

這將創建您的路線,如:

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

您還可以自動生成資源控制器以匹配您的資源路由,這將為您生成控制器文件。

php artisan make:controller Api/ArticleController --api

這將在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

methodNotAllowed異常表示您請求的 HTTP 方法不存在路由。

您的表單設置為發出POST請求,因此您的路由需要使用Route::post()來接收它。

確保在POST上設置了對郵遞員的請求

記住在任何路由更改后清除路由緩存:

php artisan route:cache

還嘗試更改郵遞員的設置:不要在標題中發送任何內容 - 我的意思是刪除 Content-Type

如果 sll 在您的域中處於活動狀態,您應該像 HTTPS://yourdomain.com 這樣的請求。 你應該檢查一下,然后再試一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM