最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

http headers - Laravel 10: getting 404 Not Found on a POST route when using Form Request Validation - Stack Overflow

programmeradmin7浏览0评论

Within a Laravel project, I want to create a POST /movies route associated with a controller that has been created with its model:

sail artisan make:model --controller --api --requests -- Movie

which has created among others the POST Form Request Validator (cf documentation Laravel 10.x) StoreMovieRequest and the controller MovieController with among others the store method:

public function store(StoreMovieRequest $request)

Finally, I declared the route within routes/api.php:

Route::post('/movies', [MovieController::class, 'store']);

But I only got a 404 Not Found, while this dummy route would succeeds:

Route::post('/movies', fn () => 'not implemented yet');

I tried many variation of the route declaration, they all produced a 404 Not Found:

Route::controller(MovieController::class)->group(function () {
    Route::get('/movies', 'index');
    Route::get('/movies/{id}', 'show');
    Route::post('/movies', 'store');
    Route::put('/movies/{id}', 'update');
    Route::delete('/movies/{id}', 'destroy');
});  // 404
Route::post('movies', [MovieController::class, 'store']);  // 404
Route::resource('movies', MovieController::class);  // 404
Route::apiResource('movies', MovieController::class);  // 404

I checked for many possible bug: bad namespace, bad file name, conflicting file name, etc. nothing has worked.

I found many stackoverflow potentially related to my problem, but none has solved this problem:

  • 404 not found on laravel post request
  • Getting 404 not found Laravel POST request
  • Laravel 8 error 404 page not found , I wonder where did I do wrong here?

Within a Laravel project, I want to create a POST /movies route associated with a controller that has been created with its model:

sail artisan make:model --controller --api --requests -- Movie

which has created among others the POST Form Request Validator (cf documentation Laravel 10.x) StoreMovieRequest and the controller MovieController with among others the store method:

public function store(StoreMovieRequest $request)

Finally, I declared the route within routes/api.php:

Route::post('/movies', [MovieController::class, 'store']);

But I only got a 404 Not Found, while this dummy route would succeeds:

Route::post('/movies', fn () => 'not implemented yet');

I tried many variation of the route declaration, they all produced a 404 Not Found:

Route::controller(MovieController::class)->group(function () {
    Route::get('/movies', 'index');
    Route::get('/movies/{id}', 'show');
    Route::post('/movies', 'store');
    Route::put('/movies/{id}', 'update');
    Route::delete('/movies/{id}', 'destroy');
});  // 404
Route::post('movies', [MovieController::class, 'store']);  // 404
Route::resource('movies', MovieController::class);  // 404
Route::apiResource('movies', MovieController::class);  // 404

I checked for many possible bug: bad namespace, bad file name, conflicting file name, etc. nothing has worked.

I found many stackoverflow potentially related to my problem, but none has solved this problem:

  • 404 not found on laravel post request
  • Getting 404 not found Laravel POST request
  • Laravel 8 error 404 page not found , I wonder where did I do wrong here?
Share Improve this question edited Mar 24 at 17:10 Christian Baumann 3,5634 gold badges24 silver badges45 bronze badges asked Mar 24 at 13:39 lavaladelavalade 3682 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I finally found the issue thanks to this github issue: when using a Form Request Validator you need to add this header

Accept: application/json

in order to Laravel to find the controller’s method.

TL;DR

// With a "Form Request Validator", the header "Accept: application/json" is mandatory, otherwise 404
public function store(StoreMovieRequest $request) {}

// With the model directly, the method/route is found even if header "Accept: application/json" is missing
public function store(Movie $movie) {}

In Postman:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论