Skip to content

Commit 5df2c3c

Browse files
author
ahmadhuss
committed
refactor: Shifted API Endpoints to APIResource
1 parent e2baf82 commit 5df2c3c

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

routes/api.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,23 @@
2020
return $request->user();
2121
});
2222

23-
// API route all methods are same like ResourceController
24-
// index, |create, store($request)|, show($id), |edit($id), update($request, $id)|, destroy($id)
23+
/**
24+
* Note: Default resourceful controller syntax is something like this
25+
* Route::resource('categories', CategoryController::class);
26+
*
27+
* The resourceful controller has 7 methods which are defined by default at the time of creation.
28+
* index, [ create, store($request) ], show($id), [ edit($id), update($request, $id) ], destroy($id)
29+
*
30+
* There is also a Api resource but the Api resource has 5 methods.
31+
* i.e.
32+
* index()
33+
* show(Category $category)
34+
* store(Request $request),
35+
* update(Category $category, StoreCategoryRequest $request)
36+
* destroy(Category $category)
37+
*/
38+
39+
/*
2540
// Now we have to create the Category API
2641
Route::get('categories', [CategoryController::class, 'index']);
2742
@@ -33,7 +48,14 @@
3348
Route::put('/categories/{category}', [CategoryController::class, 'update']);
3449
// categories delete method
3550
Route::delete('/categories/{category}', [CategoryController::class, 'destroy']);
51+
*/
3652

53+
/**
54+
* I commented all the above API routes because I want to use "API resource" which is
55+
* builtin Laravel feature and clean way for the API CRUD which behind the scenes using
56+
* all these 5 methods.
57+
*/
58+
Route::apiResource('categories', CategoryController::class);
3759

3860
// Product API
3961
Route::get('products', [ProductController::class, 'index']);

0 commit comments

Comments
 (0)