Community Post

Building REST apis using FlightPHP

Adi
👁️ 0 views
💬 comments

Introduction

Building REST apis are simpler than ever before, it's as simple as declaring a few functions and have a working api. We are about to look at a minimal PHP framework to build a basic RESTful api. FlightPHP is a fast, simple, extensible framework for PHP. This let you to quickly scaffold RESTful apis.

Installing Flight

The easiest way to install Flight would be through composer using this command

composer require mikecao/flight

Alternatively you can download the library from this Github url and extract it to you working folder.

After installation your project folder should look something similar to what is listed below.

app_folder
    - /flight               [this is the FlightPHP framework's folder]
    - index.php
    - composer.json
    - LICENSE
    - README

Fire up your Wamp, Lamp or Mamp stack and navigate to this project folder in your Browser. You should see a "hello world!" message, if not try adding the lines below to your .htaccess file in the project folder.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

Getting our feet wet

Reviewing the index.php

Open the index.php file in your favorite text editor.

<?php
require 'flight/Flight.php';

Flight::route('/', function () {
  echo 'hello world!';
});

Flight::start();
?>

Let go line by line to understand what all this means, first we require the Flight library into our script. Next we setup a BASE route using Flight::route, this method take two parameters, the first is a route url and the next is a function that's executed on that route. Finally we bootstrap Flight using it's Flight::start() method.

Routing using various HTTP methods

We saw a basic GET route, we will look at how we implement HTTP method specific routes now.

Adding the HTTP verb before the route url will filter that route, lets look at some examples below.

// This route responds to only GET requests for the '/users' url
Flight::route('GET /users', function () {
  echo 'a list of users';
});

// This route responds to only POST requests for the '/users' url
Flight::route('POST /users', function () {
  echo 'new user created';
});

This is the same for all HTTP methods, you just change GET / POST with DELETE / PUT and so on.

Passing data to our Routes

Important part of any REST api is to be able to send data dynamically. We have a few options to accomplish this in Flight.

URL Segments

// `@id` is the dynamic part of our url, we get that data as a parameter for our function
Flight::route('GET /users/@id', function ($id) {
  echo 'fetching details for user with id: ' . $id;
});

// It's the same for all other HTTP methods
Flight::route('PUT /users/@id', function ($id) {
  echo 'updating user with id: ' . $id;
});

Form Data

Large part of our data transaction is using Form Data, we are able to send large amount of data this way. Let's have a look are how we handle this type of data in Flight.

All information about the most recent Request is stored in the method Flight::request(). FormData is stored as a member variable of this method.

Flight::route('POST /posts', function () {
    $data = Flight::request()->data;  // we only want the data sent to this request so we access it's 'data' member variable
    echo 'creating new post with name: ' . $data['name'];
});

// This example combines both URL data and FormData
Flight::route('PUT /posts/@id', function ($id) {
    $data = Flight::request()->data;
    echo 'updating post with id:  ' . $id . ', name: ' . $data['name'];
});

Returning JSON

It's a common practice to send JSON formatted string as response in a RESTful api. Flight makes it very easy for us to do just that.

Flight::route('GET /allusers', function () {
    Flight::json(array('User 1', 'User 2', 'User 3')); // this returns ['User 1', 'User 2', 'User 3']
});

Conclusion

And that's it for this article, hope you got something out of it. If you get stuck anywhere, go through the source code at Github. Next time we will build the back-end of a Todo app using Flight.

Be sure to post your doubts and questions in the comments.