Created
December 6, 2016 22:09
-
-
Save miguelplazasr/be97f1e4090403f2f1aa4952e0928fe2 to your computer and use it in GitHub Desktop.
Base controller for FOSRestController
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 | |
/** | |
* Created by PhpStorm. | |
* User: miguelplazasr | |
* Date: 10/14/16 | |
* Time: 1:03 PM | |
*/ | |
namespace AppBundle\Controller\api; | |
use FOS\RestBundle\Routing\ClassResourceInterface; | |
use JMS\Serializer\SerializationContext; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
use Symfony\Component\HttpFoundation\Request; | |
use FOS\RestBundle\Controller\FOSRestController; | |
use FOS\RestBundle\Controller\Annotations; | |
use FOS\RestBundle\Util\Codes; | |
use FOS\RestBundle\View\View; | |
use FOS\RestBundle\Request\ParamFetcherInterface; | |
use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
abstract class BaseController extends FOSRestController implements ClassResourceInterface | |
{ | |
var $template = ''; | |
public function getDocumentHandler(){} | |
public function getTemplate() | |
{ | |
return $this->template; | |
} | |
/** | |
* List all Documents | |
* | |
* @ApiDoc( | |
* resource = true, | |
* output = "AppBundle\Document\Base", | |
* statusCodes={ | |
* 200 = "Returned when successful", | |
* 400 = "Returned when errors" | |
* } | |
* ) | |
* | |
* @return mixed | |
*/ | |
public function cgetAction() | |
{ | |
$data = $this->getDocumentHandler()->all(); | |
$view = $this->view($data, 200) | |
->setTemplate(":" . $this->getTemplate() . ":get.html.twig") | |
->setTemplateVar('documents') | |
->setSerializationContext(SerializationContext::create()->setGroups(["list"])) | |
; | |
return $this->handleView($view); | |
} | |
/** | |
* Get single Base | |
* | |
* @ApiDoc( | |
* resource = true, | |
* output = "AppBundle\Document\Base", | |
* statusCodes = { | |
* 200 = "Returned when successful", | |
* 400 = "Returned when the page is not found" | |
* } | |
* ) | |
* | |
* @Annotations\View(serializerGroups={"details"}) | |
* | |
* @param $id | |
* @return array | |
*/ | |
public function getAction($id) | |
{ | |
return $this->getDocumentHandler()->get($id); | |
} | |
/** | |
* Create a Base from the submitted data. | |
* | |
* @ApiDoc( | |
* resource = true, | |
* description = "Creates a new page from the submitted data.", | |
* input = "AppBundle\Form\BaseType", | |
* statusCodes = { | |
* 200 = "Returned when successful", | |
* 400 = "Returned when the form has errors" | |
* } | |
* ) | |
* | |
* @param Request $request the request object | |
* | |
* @return FormTypeInterface|View | |
*/ | |
public function postAction(Request $request) | |
{ | |
$newBase = $this->getDocumentHandler()->post( | |
$request->request->all() | |
); | |
return $newBase; | |
} | |
/** | |
* Presents the form to use to create a new Base. | |
* | |
* @ApiDoc( | |
* resource = true, | |
* statusCodes = { | |
* 200 = "Returned when successful" | |
* } | |
* ) | |
* | |
* @Annotations\View() | |
* | |
* @return FormTypeInterface | |
*/ | |
public function newAction() | |
{ | |
$form = $this->createForm($this->getDocumentHandler()->createDocumentType()); | |
$template = ":" . $this->getTemplate() . ":new.html.twig"; | |
return $this->render($template, array( | |
'form' => $form->createView() | |
)); | |
} | |
protected function getOr404($id) | |
{ | |
if (!($entity = $this->getDocumentHandler()->get($id))) { | |
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id)); | |
} | |
return $entity; | |
} | |
/** | |
* Delete Base. | |
* | |
* @ApiDoc( | |
* resource = true, | |
* statusCodes={ | |
* 200 = "Returned when successful", | |
* 400 = "Returned when errors" | |
* } | |
* ) | |
* | |
* @param $id | |
* @return bool|\Exception | |
*/ | |
public function deleteAction($id) | |
{ | |
return $this->getDocumentHandler()->delete($id); | |
} | |
/** | |
* Put Base. | |
* | |
* @ApiDoc( | |
* resource = true, | |
* input = "AppBundle\Form\BaseType", | |
* statusCodes={ | |
* 200 = "Returned when successful", | |
* 400 = "Returned when errors" | |
* } | |
* ) | |
* | |
* @param Request $request | |
* @return bool|\Exception | |
* @internal param $id | |
*/ | |
public function putAction($id, Request $request) | |
{ | |
$editBase = $this->getDocumentHandler()->put( | |
$id, | |
$request->request->all() | |
); | |
return $editBase; | |
} | |
/** | |
* Patch Base. | |
* | |
* @ApiDoc( | |
* resource = true, | |
* statusCodes={ | |
* 200 = "Returned when successful", | |
* 400 = "Returned when errors" | |
* } | |
* ) | |
* | |
* @param Request $request | |
* @return bool|\Exception | |
* @internal param $id | |
*/ | |
public function patchAction($id, Request $request) | |
{ | |
try { | |
$newBase = $this->getDocumentHandler()->patch( | |
$id, | |
$request->request->all() | |
); | |
$routeOptions = array( | |
'_format' => $request->get('_format'), | |
); | |
return $this->routeRedirectView('post_sensor_type', $routeOptions, Codes::HTTP_CREATED); | |
} catch (\Exception $exception) { | |
return $exception->getCode(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment