#3 Global API Concept
Have you practice article about Route and Controller before ? Of course now you know how to create simple API but without connected with your local server or database. Day 3, We will practice to get data from database and how to implement Global Api Concept.
A. Flow Global API Concept
Seems like another concept of creating API right ? Of course but wait, let’s dicuss it with practice. Here we go.
Let’s Create It
Go to file route on app/Config/Routes.php then add new route like below. Note : On Global Api Concept, we must use post method.
We create an post method with in IndexController on api function. Make sure you have connected your ci with an database on your server. Just go to app/config/Database.php. then add or edit configuration like below
After you connect your DB with CI, let’s create controller. In this case i will create Index.php on folder app/Controllers. Then add following code like below.
It mean, from route we will execute function api() as main function then from api() function we call another function (in this case list()). save return of that function then show result on main function.
we can create many function but the real router is on main function (in this case api()). Then how we can dynamic it ? Let’s create simple json like below.
Then we must modified our code to get data from json body. it must be like below :
Let’s test this code on postman.
If we give action value list you will execute function list() and if you give action value delete you will execute function delete() absolutly with same endpoint.
Let’s try to call data from your database with dynamic. we must modify our code like below :
Let’s test this code on postman.
Voila you got it. Do you understan with global api concept. If you have any question just write down below.
Let’s Talk About Some Project with Me
- Email : Chalidade@gmail.com
- Instagram : @sambatngoding
- Whatsapp : cutt.ly/sambatngoding