Laravel's built-in Route::resource can directly create restful-style interfaces

read table of contents

It is inevitable to write a lot of logic for adding, deleting, modifying and checking at work. Laravel's query builder is very comfortable to write, but it is still unavoidable to write a lot of repetitive code. For example, we need to implement a basic user module management function , to write the CRUD interface.

Laravel's built-in Route::resourcecan directly create restful-style interfaces, and directly add, delete, modify and query resources, which is very semantic, but in practical application, there will be the following problems:

A large number of methods need to be written, such as the logic of adding, deleting, modifying and checking, and the following seven interfaces must be implemented.

insert image description here
The resource method will follow the RESTful architecture to generate routes for user resources. The method accepts two parameters, the first parameter is the resource name and the second parameter is the controller name.

Route::resource('users', 'UsersController');

The above code will be equivalent to:

Route::get('/users', 'UsersController@index')->name('users.index');
Route::get('/users/create', 'UsersController@create')->name('users.create');
Route::get('/users/{user}', 'UsersController@show')->name('users.show');
Route::post('/users', 'UsersController@store')->name('users.store');
Route::get('/users/{user}/edit', 'UsersController@edit')->name('users.edit');
Route::patch('/users/{user}', 'UsersController@update')->name('users.update');
Route::delete('/users/{user}', 'UsersController@destroy')->name('users.destroy');

Behaviors are distinguished by HTTP verbs, front-end docking is also troublesome, PUT,POST,.GET,DELETE,PUT,PATCHetc.

Batch operations are not supported. I want to delete a hundred users.
Do I have to send DELETE requests in a https://xxx.com/api/user/{user_id}loop ?

RESTful APIs do not have an interface for bulk operations.

Guess you like

Origin blog.csdn.net/weiguang102/article/details/123854265