Created
February 26, 2018 18:40
-
-
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
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 | |
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