Step1 - Create a REST API Endpoint

Follow the steps below if you need to create an API end point to define the interactions between multiple software.

*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!;

All your API's must be stored in the api directory. Inside /src/routes/ we begin by creating a file for our API endpoint

Step 2 - Define routes API Endpoint

There are various methods we can use when creating our API Endpoint.

To create a Get Route


<?php
\Tina4\Get::add("/api/cars", function(\Tina4\Response $response) {

  $cars = ["one", "two", "three"];

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

To create a Post Route


<?php
\Tina4\Post::add("/api/cars", function(\Tina4\Response $response) {

  $cars = ["one", "two", "three"];

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

Other methods you could use are:


<?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(...);


Its important to keep in mind and ensure that the paths are correct:


Secure API Endpoint

You might want to secure your REST API to avoid exposing data and data breaches. After you have created your REST API, you can easliy secure it by adding “@secure” in your annotation.

Here's an example of securing your API Endpoint:


/**
* @description My first API
* @summary Returns list of cars
* @tags Cars
* @secure
*/

Step 3 - Annotate our Endpoint

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) {

  $cars = ["one", "two", "three"];

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

Step 4 - Accessing/Testing our Endpoint

To access our enpoint we will navigate to the relevant path in our browser. E.g. http://localhost:7145/api/items



Also the swagger UI for all your annotated endpoints can be found at http://localhost:7145/swagger

Powered by ComboStrap