Building a basic RESTful API with Fat Free Framework

Written by on April 7, 2015

Building a basic RESTful API with Fat Free Framework

Last year I wrote an article covering best available PHP RESTful micro frameworks, now I want to show you how to create a simple RESTfull using the Fat-Free Framework. This article will cover only installation, configuration, and basic example. Next one will include more Fat-Free information and additional working examples. I will include Windows and Linux (Debian/Ubuntu) setup, one after another.

Before we start this tutorial make sure, you have everything installed/configured to handle PHP and Fat-Free. To make it easier, we will use preconfigured development environments, WAMP for Windows and LAMP for Linux. I do not advise using WAMP/LAMP for your production environment.

Note: If this tutorial was helpful, you need further clarification, something is not working or you have a request for another Ionic post? Furthermore, leave me a comment below if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here. Feel free to comment below, subscribe to my blog, mail me to dragan.gaic@gmail.com. Thanks and have a nice day!

Useful Links

Official Site GitHub User Guide

API Reference

Table of Contents

Note: If you have already installed WAMP/LAMP + Composer just jump to the Fat Free Framework installation segment (links are provided below).

Windows Preparation
Linux Preparation
Fat Free Example

Windows Preparation

1a. Install WAMP

Go to the official WAMP site and get the latest version, or any other if you don’t want the use latest version. Depending on your Windows platform (32 bit or 64 bit), select Windows Installer (.msi) and download it.

Download Link

Fat Free 0001

When you click on download directly current page will change to sourceforge.net download location, don’t click on anything, download will start automatically. Begin the installation when download completes. During this process, you will be prompted to select your default browser, do so.

If you receive this error during the installation you should also install Visual Studio 2012 VC 11 vcredist_x64/86.exe (again select 32 or 64 bit version):

Fat Free 0002

Download Link

Restart WAMP or Windows. If you did everything correctly, WAMP icon (in the icon tray) should glow green, and you will be able to open your browser and go to the localhost. For the purpose of this article, WAMP is installed at this location:

c:\WAMP\

and PHP should be installed at this location:

c:\WAMP\bin\php\php5.5.12\

Copy this path and add it to your environment PATH. Take a look at this tutorial if you don’t know how to do that, you will find examples for Windows XP, Windows 2000 and Windows 7. It should look like this:

Fat Free 0003

Do the same thing with curl, find it here:

c:\WAMP\bin\apache\apache2.4.9\extra-modules\mod_security-2.8.0\mlogc\

If you are using different WAMP version then this there’s a good chance you won’t find curl in that folder. In this case just go to WAMP root folder and search for curl.exe. Copy this path and add it to your environment PATH, it should look like this:

Fat Free 0004

2a. Install Composer

First create a folder for the Composer. Go to this location:

c:\WAMP\bin

And create a folder named:

c:\WAMP\bin\composer

Copy this path and add it to your environment PATH, it should look like this:

Fat Free 0005

Open Windows command prompt (at this location: c:\WAMP\bin\) and execute:

curl -sS "http://getcomposer.org/installer" | php -- --install-dir=composer

If you did everything correctly, output should look like this:

#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: C:\WAMP\www\composer\composer.phar
Use it: php composer/composer.phar

While you are still in the command prompt, go to this newly created folder: C:\WAMP\bin\composer and execute this:

echo @php "%~dp0composer.phar" %*>composer.bat

3a. Install Fat Free

First create a project folder. Go to this location:

c:\WAMP\www\

And delete everything you find there, do this only once after the initial WAMP installation. The www folder (sometimes it’s also called public_html or htdocs) is the web root for your primary domain name. This means that www is the folder where you put all website files which you want to appear when someone types your primary domain (the one you provided when you signed up for hosting, or localhost in our case). Our project folder name will be (name it any way you want):

c:\WAMP\www\fatfree_demo

Open command prompt and go to the project folder. Execute this line:

composer require bcosca/fatfree *

File structure will look like this:

fatfree_demo
    --->composer.json
    --->composer.lock
    --->vendor
        --->composer
        --->bcosca
        --->autoload.php

4a. Activate mod_rewrite

Start WAMP server by clicking on its icon (if it’s already not running). Wamp icon should appear in Windows system tray. Left click on it, browse through Apache -> Apache modules -> end finally
enable rewrite_modul.

Restart Apache server.

Linux Preparation

This tutorial has been tested on a single VPS DigitalOcean 512MB droplet, Ubuntu 14.04 x64. It is much shorter than a Windows preparation tutorial … go figure.

1b. Install LAMP

Execute this first:

sudo apt-get update

If you don’t update package lists from the repositories, you will probably receive this error: tasksel: aptitude failed (100). Now we’ll install the LAMP stack:

sudo  tasksel

Taskel will open a GUI menu, select only LAMP server, press OK, wait some time and finally enter MySQL root password when GUI prompts you:

Fat Free 0006

Fat Free 0007

Fat Free 0008

If you still can’t manage to finish the installation (error mentioned above) install it directly like this:

sudo tasksel install lamp-server

2b. Install Curl

Do this step only if haven’t previously installed Curl:

sudo apt-get install curl

Fat Free 0009

3b. Install Composer

Execute these lines:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

It should look like this:

Fat Free 0010

Continue Reading

Categories

7 thoughts on “Building a basic RESTful API with Fat Free Framework”

  1. Hi,

    this looks pretty useful but I couldn’t make it work. It seems that some steps are excluded (setting up fatfree.localhost, .htaccess, location of index.js). I figured these out but still getting ajax error (line 11, parseJSON).

    Any chance of supplying the full source?

  2. I'm struggling with the same (or similar) problem. I'm running on Linux. The symptom is "Unexpected token <" and is coming from index.js line 33, then line 11 so I assume its the same problem

    • I forgot to mention mod_rewrite modul needs to be enabled. I have expanded my article with correct .htaccess and vhost file. You can test it if you go to this links:

      fatfree.gajotres.net
      fatfree.gajotres.net/1
      fatfree.gajotres.net/2

  3. Does this code even work ? It doesnt for me ?… On a linux box here.
    LAMP is working / runnig.

    At the very least you could have put up some screenshots to show how this app looks….
    What a bummer ….!!!

Leave a Reply