Skip to content

Instantly share code, notes, and snippets.

@brianium
Last active January 3, 2016 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianium/8408639 to your computer and use it in GitHub Desktop.
Save brianium/8408639 to your computer and use it in GitHub Desktop.
Richardson maturity model: Resources with Silex
<?php
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
$app = new Application();
$app->post('/order', function(Request $request) {
$service = new OrderService();
$order = OrderFactory::fromArray($request->request->all());
$service->setOrder($order);
return json_encode($service->placeOrder());
});
$app->post('/order/{id}/status', function(Application $app, $id) {
$order = OrderRepository::getById($id);
if (! $order)
$app->abort('404', "Order with id $id not found");
return json_encode($order->getStatus());
});
@brianium
Copy link
Author

We now interact with specific resources instead of a single service endpoint.

If we want to create a new order we might do something like this:

POST /order HTTP/1.1

{
  "beer_id": "54cdef43",
  "tip": 5.99
  "credit_card": {
    "number": 4444444444444441,
    "cvc": 123,
    "expires": "10/15"
  }
}

And you will get an order response like so:

{
  "id": "5cf4de3",
  "beer_id": "54cdef43",
  "tip": 5.99,
  "total": 12.39 
}

And if we want to check on the status of an order, we interact with the status resource:

POST /order/5cf4de3/status HTTP/1.1

And we would get a response like:

{
  "id":"54cder",
  "order_id": "5cf4de3",
  "last_updated":"2014-05-06:11-11-11",
  "status":"pending"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment