Creating a basic RESTful API with Silex

Written by on April 7, 2015

Creating a basic RESTful API with Silex

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 Silex framework. This article will cover only installation, configuration, and basic example. Next one will include more Silex 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 Silex. 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 Documentation

Documentation as a PDF Book

Table of Contents

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

Windows Preparation
Linux Preparation
Silex 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

Silex 0001

When you click on download directly current page will change to sourceforge.net download location, don’t click on anything, a 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):

Silex 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:

Silex 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:

Silex 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:

Silex 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 Silex

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\silex_demo

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

composer require silex/silex *

File structure will look like this:

silex_demo
    --->composer.json
    --->composer.lock
    --->vendor
        --->composer
        --->pimple
        --->psr
        --->silex
        --->symfony
        --->autoload.php

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:

Silex 0006

Silex 0007

Silex 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

Silex 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:

Silex 0010

Continue Reading

Categories

3 thoughts on “Creating a basic RESTful API with Silex”

  1. Thank you for the article.
    Just wanted to point out if you are blindly copying and pasting (like I was) create a subdirectory called 'js' under the silex_demo dir and drop the index.js there.

Leave a Reply