Last active
December 24, 2015 16:19
-
-
Save fdubost/6827610 to your computer and use it in GitHub Desktop.
REST with FOSRestBundle 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\Request; | |
use FOS\RestBundle\Controller\Annotations as Rest; | |
use FOS\RestBundle\Request\ParamFetcher; | |
use FOS\RestBundle\View\View; | |
use FOS\RestBundle\Controller\Annotations\Route; | |
use M6\Bundle\MyRESTBundle\Form\TalkType; | |
use M6\Bundle\MyRESTBundle\Entity\Talk; | |
class MyRESTController extends Controller | |
{ | |
/** | |
* @QueryParam(name="session", requirements="[0-9}{4}-[0-9]{2}-[0-9]{2}", strict="true", nullable="true", description="Talks session") | |
*/ | |
public function getTalksAction(ParamFetcher $paramFetcher) { | |
if ($session = $paramFetcher->get('session')) { | |
$talks = $this->get('doctrine')->getRepository('MyRESTBundle:Talk')->findBy(array( | |
'session' => new \DateTime($session) | |
)); | |
} else { | |
$talks = $this->get('doctrine')->getRepository('MyRESTBundle:Talk')->findAll(); | |
} | |
return new View($talks, 200); | |
} | |
/** | |
* @Route(requirements={"id"="\d+"}) | |
*/ | |
public function getTalkAction($id) { | |
$talk = $this->get('doctrine')->getRepository('MyRESTBundle:Talk')->find($id); | |
if (is_null($talk)) { | |
return new View('Talk not found', 404); | |
} | |
return new View($talk, 200); | |
} | |
public function postTalksAction(Request $request) { | |
$talk = new Talk(); | |
$form = $this->createForm(new TalkType(), $talk); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$em = $this->get('doctrine')->getEntityManager('rest'); | |
$em->persist($talk); | |
$em->flush(); | |
return new View($talk, 201, array( | |
'Location' => $this->generateUrl('get_talk', ['id' => $talk->getId()]) | |
)); | |
} | |
return new View($form, 422); | |
} | |
/** | |
* @Route(requirements={"id"="\d+"}) | |
*/ | |
public function putAction($id, Request $request) | |
{ | |
$talk = $this->get('doctrine')->getRepository('MyRESTBundle:Talk')->find($id); | |
if (is_null($talk)) { | |
return $this->view('Talk not found', 404); | |
} | |
$form = $this->createForm(new TalkType(), $talk, ['method' => 'PUT']); | |
$form->handleRequest($request); | |
if ($form->isValid()) { | |
$em = $this->get('doctrine')->getEntityManager('rest'); | |
$em->flush($talk); | |
return $this->view(null, 204); | |
} | |
return $this->view($form, 422); | |
} | |
/** | |
* @Route(requirements={"id"="\d+"}) | |
*/ | |
public function deleteTalkAction($id) | |
{ | |
$talk = $this->get('doctrine')->getRepository('MyRESTBundle:Talk')->find($id); | |
if (is_null($talk)) { | |
return $this->view('Talk not found', 404); | |
} | |
$em = $this->get('doctrine')->getEntityManager('lftrest'); | |
$em->remove($talk); | |
$em->persist(); | |
return $this->view(null, 204); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment