Creating API's With Tina4




Follow the steps below if you need to create an API end point to define the interactions between multiple software. This will be required as your application, system or webpage will require multiple interactive systems communicating with each other. Your API will provide the instructions on how these systems interact with each other. It handles things just as your HTTP requests, web applications, information and specifications.

Please Note : After installing Tina4, you may delete the test.php file located inside the api folder.

The REST end point is easily added, with an anonymous method to handle the request, the anonymous method should have the response variable.

The request exposes more information which comes from the browser, in the case of parameters passed to it. You should always return the $response object!

Step 1 - Create API file & sub-directories

All your API's must be stored in the API directory. For this example we will make the files and sub-directories for “cars” and “characters”.

Create 2 directories inside the API folder and name it “cars” and “characters”. Now create php files inside those sub-directories (eg. name the file cars and the other characters).

Step 2 - Define routes

Now that the API files has been created, we must define our Routes. There are various methods which can be used.

It is IMPORTANT to ensure that the paths are correct : “/api/characters”

To know more about Routing go to the Routing page.

Get Method

This is how to create a Get route with a response and request using TIna4.

<?php

\Tina4\Get::add("/api/cars", function (\Tina4\Response $response, \Tina4\Request $request) {

    $cars = ["Rolls Royce", "Aston Martin", "Bugatti"];

return $response ($cars, HTTP_OK, APPLICATION_JSON);
});

Post Method

This is how to create a Post route with a response and request using TIna4.

<?php

\Tina4\Post::add("/api/characters", function (\Tina4\Response $response, \Tina4\Request $request) {
    $characters = ["Clancy Gilroy", "Peter Griffin", "Broden Kelly"];
return $response ($characters, HTTP_OK, APPLICATION_JSON);
});

Other Routes Methods

Other methods you may need to use include:

<?php

//Other methods you can test
\Tina4\Post::add(...);

\Tina4\Patch::add(...);

\Tina4\Put::add(...);

\Tina4\Delete::add(...);

//You guessed it - It takes every method - GET, POST, DELETE, PUT, PATCH, OPTIONS
\Tina4\Any::add(...);

Step 3 - Annotate API

It is useful to add comments to your scripts for future use (e.g. if someone else has to work on your code). Now we will need to annotate the end point we created. This will be a description of the end point which will be used in Swagger. Its as easy as adding the following lines above the end point you created:

<?php

/**
* @description My first API
* @summary Returns list of cars
* @tags Cars
*/
\Tina4\Get::add("/api/cars", function (\Tina4\Response $response, \Tina4\Request $request) {

    $cars = ["Rolls Royce", "Aston Martin", "Bugatti"];

return $response ($cars, HTTP_OK, APPLICATION_JSON);
});

Step 4 Test API

Now we can test to check if our API is working. Spin up a web server by running this in your IDE terminal or command line:

composer start

Now that the web server is running, add “/swagger” to the URL:

_

A Swagger page will load and you will see the following which will include the data and annotations within the script created:

_

Next click on the API and then click on “Try It Out” button:

_

Thereafter, click on the execute button and you will see the following responses in the browser:

_

Get Method Result

_

Post Method Result

_

You just created a basic API using Tina4. You are on your way to becoming a serious Code Ninja or Kunoichi.

You will notice that the Get method retrieved the information, whereas the Post method gave a 403 Forbidden error.

Please reach out to the developers if you have any questions or suggestions.

For more information on how to secure your API's, please click on link. For more information on API's please look at FreeCodeCamp or HowToGeek.

Powered by ComboStrap