Fork me on GitHub
David Makin's avatar

Creating a simple REST application with Silex part 2.

In part 1 you installed Silex and setup 2 routes, / and /{stockcode}. Now lets expand upon those by adding a POST and a DELETE route.

The 2 routes we created use GET but to make your application truly useful you will want to use at least 1 more type and that is POST.

Adding a POST route

Let us add a route to allow new products to be added. To our index.php file we will need to add the following somewhere before the $app->run(); line.

 1 <?php
 2 $app->post('/', function (Silex\Application $app, Symfony\Component\HttpFoundation\Request $request) {
 3 
 4     $name = $request->get('name');
 5     $quantity = $request->get('quantity');
 6     $description = $request->get('description');
 7     $image = $request->get('image');
 8     
 9     // Code to add the toy into the toy db
10     // and return a toy id
11     //$toy_id = create_toy($name, $quantity, $description, $image);
12     //$toy = get_toy($toy_id);
13     
14     // For now lets just assume we have saved it
15     $toy = array(
16         '00003' => array(
17             'name' => $name,
18             'quantity' => $quantity,
19             'description' => $description,
20             'image' => $image,
21         )
22     );
23     
24     // Useful to return the newly added details
25     // HTTP_CREATED = 200
26     return new Symfony\Component\HttpFoundation\Response(json_encode($toy), HTTP_CREATED);
27 });

Deleting an item of stock

At some point we will want to delete items. For that you need a DELETE route

 1 <?php
 2 $app->delete('/{toy_id}', function (Silex\Application $app, Symfony\Component\HttpFoundation\Request $request, $toy_id) {
 3     
 4     if (delete_toy($toy_id)) {
 5         // The delete went ok and we can now return a no content value
 6         // HTTP_NO_CONTENT = 204
 7         $responseMessage = '';
 8         $responseCode = Symfony\Component\HttpFoundation\Response::HTTP_NO_CONTENT;
 9     } else {
10         // Something went wrong
11         $responseMessage = 'reason for error';
12         $response_code = Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR;
13     }
14     
15     return new Symfony\Component\HttpFoundation\Response($responseMessage, $responseCode);
16 });

The same route but different verbs

You might have noticed that we now have multiple URLs in the application that are the same, /. Acutally they are not, one uses the GET verb, one the POST and one DELETE. How does it know which to use? Well that depends on the request type it receives and explaining how to send POST, DELETE, PUT etc is a big post in itself. Have a read of REST API Tutorial to learn which verb to use in which situation.

How do I test these routes

For GET you can simply browse to it in your webclient of choice but the others are a little more difficult.

If you you are comfortable with the command line you can use curl. Something like this should do

curl -i -H "Accept: application/json" -X DELETE http://127.0.0.1/00002

Where:
-i show response headers
-H pass request headers to the resource
-X pass a HTTP method name

A much nicer way if to use a REST client, PHPStorm has one builtin or you can get a browser addon, RESTClient for Firefox or Postman for Chrome. If you want to consume a route in your client code you can use plain old php fopen, curl or the nice Guzzle library.

Part 3

Now that we have several routes in index.php it is starting to look messy and you should be able to see that adding everything into 1 file will cause us headaches down the road in terms of readability and maintainability. Part 3 will look at moving these into separate libraries.

Links

part 1
part 3

REST API Tutorial RESTClient
Postman
Guzzle library

comments powered by Disqus