Skip to content

Instantly share code, notes, and snippets.

@NandoKstroNet
Created February 26, 2018 18:40
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 NandoKstroNet/07668dd8e07f2065de6cb0a83a6fd266 to your computer and use it in GitHub Desktop.
Save NandoKstroNet/07668dd8e07f2065de6cb0a83a6fd266 to your computer and use it in GitHub Desktop.
BeerController criado na serie sobre API com Symfony 3.4 da Code Experts Learning
<?php
namespace APIBundle\Controller;
use APIBundle\Entity\Beer;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
/**
* @Route("/beers")
*/
class BeerController extends Controller
{
/**
* @Route("/", name="api_beers")
*/
public function index()
{
$beers = $this->getDoctrine()
->getRepository('APIBundle:Beer')
->findAll();
$beers = $this->get('jms_serializer')->serialize($beers, 'json');
$response = new Response($beers, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* @Route("/{id}", name="api_beers_show")
*/
public function show(Beer $beer)
{
$beers = $this->get('jms_serializer')->serialize($beer, 'json');
$response = new Response($beers, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment