Last active
January 3, 2016 04:28
-
-
Save brianium/8408792 to your computer and use it in GitHub Desktop.
Richardson maturity model: Resources + verbs with Silex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We now introduce an HTTP interface that allows us to distinguish between safe and unsafe, cacheable and un-cacheable, idempotent and non-idempotent.