2. Final Report Tasniflagich


 Routing 3.3.1 GET, POST, PUT, DELETE methods


Download 3.88 Mb.
Pdf ko'rish
bet9/15
Sana25.07.2023
Hajmi3.88 Mb.
#1662428
1   ...   5   6   7   8   9   10   11   12   ...   15
Bog'liq
2.Final Report Tasniflagich

3.3 Routing
3.3.1 GET, POST, PUT, DELETE methods
For client applications we have to create a reliable REST API. To define routes we use
Route class provided by Laravel. To link GET route and function to handle, we should use the
get method of Route class in api.php file which located in “routes” folder
Figure 4. define get method
Figure 4 represents code to define route name api.adm.tasniflagich.mf.uz/prefetch and
handler as preFetchSearch in FrontController class.
TTPU
14
2022


To link POST, PUT, DELETE routes and functions to handle, we should use the post, put
and delete methods of Route class in the api.php file which is located in the “routes” folder.
Figure 5 represents code to define route name api.adm.tasniflagich.mf.uz/category and
handlers as store, update and delete in CategoryController class. As we can see there is the same
route name but different methods and different handlers.
Figure 5. define post, put and delete methods
3.3.2 apiResource method
Sometimes we need to create routes to manipulate models. The model must be able to
Create, Read, Update, and Delete resources. The Route class has a special method for this case
named “apiResource”. The method apiResource link predefined Controller methods as index,
show, store, update and delete with routes and related request methods.
Figure 6. CRUD in 1 row via apiResource
In figure 6 with 1 method we created 5 routes:
1. GET /category - index of all available categories
2. GET /category/{id} - data of category by id
3. POST /category - route to create category
4. PUT /category/{id} - route to edit category by id
5. DELETE /category/{id} - route to delete category by id
TTPU
15
2022



Download 3.88 Mb.

Do'stlaringiz bilan baham:
1   ...   5   6   7   8   9   10   11   12   ...   15




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling