I have this schema for my REST API urls:
Verb Url Method
GET /tasks findAll
GET /tasks/{id} findOne
POST /tasks create
PUT /tasks/{id} update
DELETE /tasks/{id} deleteOne
DELETE /tasks deleteAll
Is there a way for override the default method of Route Resource Laravel built-in methods (store,create,edit etc...) and create with a single line my custom route associated with my controller?
For example:
Route::resource('/tasks', 'TasksController');
Instead of:
Route::get('/tasks', 'TasksController@findAll');
Route::get('/tasks/{id}', 'TasksController@findOne');
Route::post('/tasks', 'TasksController@create');
Route::put('/tasks/{id}', 'TasksController@update');
Route::delete('/tasks', 'TasksController@deleteAll');
Route::delete('/tasks/{id}', 'TasksController@deleteOne');
I have solved making these steps changing the ResourceRegistrar.php class, this achieve my request. (suggest by @Thomas Van der Veen):
1) I have replaced $resourceDefaults array with my desires methods:
2) After I have create the methods which execute the actions, deleting the olders.
That's it, works very well!
You should take a look at Laravel documentation here: https://laravel.com/docs/5.3/controllers#resource-controllers
About deleteAll action, Laravel doesn't provide a default call for that. I would recommend to avoid this.
In order to create a resource controller just type on your terminal this command:
This will create a controller with default CRUD actions you'll have to fill.
Then in routes.php just add:
You'll be able to send calls to server like you described.