Last active
December 24, 2015 15:59
-
-
Save fdubost/6825054 to your computer and use it in GitHub Desktop.
Listing REST example
This file contains 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 M6\Bundle\MyRESTBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Response; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use M6\Bundle\MyRESTBundle\Form\TalkType; | |
use M6\Bundle\MyRESTBundle\Entity\Talk; | |
class MyRESTController extends Controller | |
{ | |
/** | |
* Simply list talks | |
* | |
* @Route("/talks") | |
* @Method("GET") | |
*/ | |
public function simplyListAction() { | |
$talks = $this->get('doctrine')->getRepository('M6LftRestBundle:Talk')->findAll(); | |
$response = $this->render('M6LftRestBundle:Default:list.xml.twig', array('talks' => $talks)); | |
$response->headers->set('Content-Type', 'text/xml'); | |
return $response; | |
} | |
/** | |
* List talks with a filtering params | |
* | |
* @Route("/talks) | |
* @Method("GET") | |
*/ | |
public function filteredListAction() { | |
$session = $this->getRequest()->get('session'); | |
if ($session && preg_match('#[0-9}{4}-[0-9]{2}-[0-9]{2}#', $session)) { | |
$talks = $this->get('doctrine')->getRepository('M6LftRestBundle:Talk')->findBy(array( | |
'session' => new \DateTime($session) | |
)); | |
} elseif ($session) { | |
return new Response('Bad parameter', 400); | |
} else { | |
$talks = $this->get('doctrine')->getRepository('M6LftRestBundle:Talk')->findAll(); | |
} | |
$response = $this->render('M6LftRestBundle:Default:list.xml.twig', array('talks' => $talks)); | |
$response->headers->set('Content-Type', 'text/xml'); | |
return $response; | |
} | |
/** | |
* List talks with a filtering params and format choice | |
* | |
* @Route("/talks.{_format}", requirements={"_format" = "json|xml"}, defaults={"_format" = "~"}) | |
* @Method("GET") | |
*/ | |
public function formatListAction() { | |
if ($session = $this->getRequest()->get('session')) { | |
$talks = $this->get('doctrine')->getRepository('M6LftRestBundle:Talk')->findBy(array( | |
'session' => new \DateTime($session) | |
)); | |
} else { | |
$talks = $this->get('doctrine')->getRepository('M6LftRestBundle:Talk')->findAll(); | |
} | |
if ($this->getRequest()->get('_format') == 'json') { | |
$jsonTalks['talks'] = array(); | |
foreach ($talks as $talk) { | |
$jsonTalk = new \stdClass(); | |
$jsonTalk->id = $talk->getId(); | |
$jsonTalk->author = $talk->getAuthor()->getName(); | |
$jsonTalk->title = $talk->getTitle(); | |
$jsonTalk->session = $talk->getSession()->format('m-d-Y'); | |
$jsonTalks['talks'][] = $jsonTalk; | |
} | |
$response = new Response(json_encode($jsonTalks)); | |
$response->headers->set('Content-Type', 'application/json'); | |
} else { | |
$response = $this->render('M6LftRestBundle:Default:list.xml.twig', array('talks' => $talks)); | |
$response->headers->set('Content-Type', 'text/xml'); | |
}; | |
return $response; | |
} | |
/** | |
* @Route("/talks") | |
* @Method("POST") | |
*/ | |
public function addAction() { | |
$talk = new Talk(); | |
$form = $this->createForm(new TalkType(), $talk); | |
$form->submit($_POST); | |
if ($form->isValid()) { | |
$em = $this->get('doctrine')->getEntityManager('lftrest'); | |
$em->persist($talk); | |
$em->flush(); | |
$response = $this->render('M6LftRestBundle:Default:detail.xml.twig', array('talk' => $talk), 201); | |
return $response; | |
} | |
return new Response('Invalid data', 422); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment