Fork me on GitHub
David Makin's avatar

Creating a simple REST application with Silex.

So you have come to the conclusion that, rather than having the code to update customer details and that little fix to convert the date from GMT to UTC should happen in once place rather than being copied into several pieces of code, a REST service is the answer. Perhaps you just think you should know how to create a REST service. Either way this post is for you.

In this post we will start off with a blank directory and go through installing Silex and getting it to serve a couple of simple routes.

Lets Start

For this example I will be using a directory called toyshop.

Install Silex

There are multiple ways to install Silex but lets use Composer.

Inside your working directory run the command

composer require silex/silex ~1.1

With this command you have told it to: require adds a new package to the project silex/silex the name of the package in the format vendor/package ~1.1 the version of the package, ~1.1 means >=1.1, <2.0

As this is running you will see that it goes off and installs libraries you didn't request, don't worry this is meant to happen as Silex relies upon them to run.

Once it is finished you will have a file structure like this.

toyshop/
       composer.json
       composer.lock
       vendor/
             ...

All the Silex code is stored inside of vendor with the 2 composer files contain the information about which packages and versions are installed.

Composer.json

Lets have a look at composer.json, it should look something like this

{
    "require": {
        "silex/silex": "~1.1"
    }
}

Composer.lock

This file contains a list of every single package installed as well as the version. This is useful as it means you don't need to commit the vendor/ folder into VCS.

As long as you check in composer.json and composer.lock into your VCS of choice as once the code is checked out you can just run composer install and it will download the exact have versions you currently have installed.

Creating our first Routes

As this example is a toyshop let us think what it will need. People browsing our site will want to see the presents so lets create a route to do just that.

Let us create the file index.php and it should look like this

 1 <?php
 2 require_once __DIR__.'/vendor/autoload.php';
 3 
 4 $app = new Silex\Application();
 5 // Please set to false in a production environment
 6 $app['debug'] = true;
 7 
 8 $toys = array(
 9     '00001'=> array(
10         'name' => 'Racing Car',
11         'quantity' => '53',
12         'description' => '...',
13         'image' => 'racing_car.jpg',
14     ),
15     '00002' => array(
16         'name' => 'Raspberry Pi',
17         'quantity' => '13',
18         'description' => '...',
19         'image' => 'raspberry_pi.jpg',
20     ),
21 );
22 
23 $app->get('/', function() use ($toys) {
24 
25     return json_encode($toys);
26 });
27 
28 $app->get('/{stockcode}', function (Silex\Application $app, $stockcode) use ($toys) {
29 
30     if (!isset($toys[$stockcode])) {
31         $app->abort(404, "Stockcode {$stockcode} does not exist.");
32     }
33     return json_encode($toys[$stockcode]);
34 });
35 
36 $app->run();

There are 2 routes in this / and /{stockcode}

Route /

This route is designed so you can GET all the toys. All it does is encode them as JSON and return that to the requester.

To allow the anonymous function to access the $toys array we need to import it by adding use ($toys) at the end of the definition but before the opening {.

Route /{stockcode}

This will GET the information about a particular toy as passed in via the stockcode parameter. As you can see it has a slightly more complicated function header to accommodate this.

As well as use ($toys) we are passing in a couple of variables to the function itself Silex\Application $app & $stockcode.

Silex\Application $app is the Silex application and helps us provide a nice 404 page if an invalid stockcode is provided. $stockcode is the Id of the toy we are interested in. As you can see it matches {stockcode} in the route.

View your routes.

Once you have setup your webserver of choice to route everything via index.php you can browse to your routes at http://toyshop/ & http://toyshop/00001 and see the toys.

http://toyshop/00001 will display

{"name":"Raspberry Pi","quantity":"13","description":"...","image":"raspberry_pi.jpg"}

This is now in a format suitable for your frontend shop or backend telephone operator system to json_decode and display in a friendly html format.

Why Silex?

Good question. The answer is because as it is a micro-framework it doesn't try to get in the way by forcing it's own preferred method of working on me and it just happens to be what I am currently using. If you wanted you could use Slim, any other micro-framework or even a full fat framework like Symfony or Zend.

Links

part 2
part 3

comments powered by Disqus