Last active
January 3, 2016 04:19
-
-
Save brianium/8408639 to your computer and use it in GitHub Desktop.
Richardson maturity model: Resources 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->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()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
And you will get an order response like so:
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: