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?
1 Answer
Reset to default 0I 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: