Пример #1
0
$app->get('/posts', function () use($app) {
    $em = $app['orm.em'];
    $results = $em->getRepository('App\\Entities\\Post')->findAll();
    $hydrator = new CollectionExtractor(new DoctrineHydrator($em));
    return $app->json($hydrator->extract($results));
});
$app->get('/posts/{id}', function ($id) use($app) {
    $em = $app['orm.em'];
    $post = $em->getRepository('App\\Entities\\Post')->find($id);
    $hydrator = new DoctrineHydrator($em);
    return $app->json($hydrator->extract($post));
});
$app->post('/posts', function (Request $request) use($app) {
    $em = $app['orm.em'];
    $post = new Post();
    $post->setTitle($request->get('title'));
    $post->setContent($request->get('content'));
    $em->persist($post);
    $em->flush();
    return $app->json($hydrator->extract($post));
});
$app->put('/posts/{id}', function ($id, Request $request) use($app) {
    $em = $app['orm.em'];
    $post = $em->getRepository('App\\Entities\\Post')->find($id);
    $post->setTitle($request->get('title'));
    $post->setContent($request->get('content'));
    $em->persist($post);
    $em->flush();
    return $app->json($hydrator->extract($post));
});
$app->delete('/posts/{id}', function ($id) use($app) {