Creating a REST API in PHP (in 5 minutes or less) Part I

Christian Estay
2 min readJun 25, 2024

--

In this article, we are going to do a simple exercise of creating a REST API using the Flight PHP library.

Flight is a framework written in PHP (requires PHP 7.4+) which allows us to generate a REST API ready to be invoked in a few lines of code.

Flight in action!

In 5 lines of code we have an API that returns a “Hello World”
The first thing is to download the framework from the github repository.

Once downloaded, we copy the content within our project, then we generate an .htaccess (in Apache) to process the requests.

.htaccess for Flight

Lastly, we reference the library within our project.

And that’s it! We are ready to start developing some methods.

The example shows how the same URI is used with different HTTP methods to execute different functions.

Each method is defined by the static route function, which has two parameters, first, the HTTP method and the associated URI are defined in a single text string, and then, the anonymous function (later we will see other forms) that will be invoked in each when matching with the rule.

In the next article we will see in greater depth how to separate more robust and elegant development into layers and complement Flight with Redbeans for very easy data access.

--

--