Skip to content

Instantly share code, notes, and snippets.

@brianium
Last active January 3, 2016 04:28
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/8408792 to your computer and use it in GitHub Desktop.
Save brianium/8408792 to your computer and use it in GitHub Desktop.
Richardson maturity model: Resources + verbs 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->get('/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());
});
$app->put('/order/{id}/status', function(Application $app, Request $request, $id) {
$order = OrderRepository::getById($id);
if (! $order)
$app->abort('404', "Order with id $id not found");
$order->setStatus($request->request->get('status');
OrderRepository::update($order);
return json_encode($order->getStatus());
});
@brianium
Copy link
Author

We now introduce an HTTP interface that allows us to distinguish between safe and unsafe, cacheable and un-cacheable, idempotent and non-idempotent.

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