I was asking myself if it was a bad practice to send every ajax request I use on my Laravel 8 project with the HTTP Post method.
What I mean is that, according to Laravel 8 documentation on routing (if I understood everything well), you should use the appropriate HTTP request :
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
A French tutorial on the routes explains this (translated by myself) :
GET : ask for a resource that never changes,
POST : update or create a resource (often wrongly used when PUT is needed),
PUT : create or replace a resource,
PATCH : partialy update a resource,
DELETE : delete a resource.
The “problem”, if there is one, is that in my code in almost only use HTTP post requests for AJAX in my routes/web.php file, like this :
Route::post('mails/update/{id}', 'AppHttpControllersMailController@update');
Route::post('mails/sendMail/', 'AppHttpControllersMailController@sendMailTest')->name('mails.sendMailTest');
Route::post('mails/sendInstantMail/', 'AppHttpControllersMailController@sendInstantMail')->name('mails.sendInstantMail');
Route::post('mails/getRouteList/', 'AppHttpControllersMailController@getRouteList')->name('mails.getRouteList');
According to what I read, I should probably use PUT or PATCH for my update method. But with POST it works. Is it a bad practice to continue to use POST?
If I guess right, AJAX is only a way to do HTTP request, but in fact it is still the same idea behind the request, so I should change my request methods.
I am really sorry if my question is dumb, and thanks for your help!
Sources :
Laravel 8 – Route documentation about the router methods : https://laravel.com/docs/8.x/routing#available-router-methods
French tutorial on Laravel routing see “Les méthodes” chapter : https://laravel.sillo.org/cours-laravel-8-les-bases-le-routage/
English translated tutorial with Google Translate (See “The methods” chapter) : https://laravel-sillo-org.translate.goog/cours-laravel-8-les-bases-le-routage/?_x_tr_sl=fr&_x_tr_tl=en&_x_tr_hl=fr&_x_tr_pto=wapp