Skip to content

Instantly share code, notes, and snippets.

@mradhi
Last active September 19, 2017 13:15
Show Gist options
  • Save mradhi/a95e416f2194ee01038d399d5e06d7e0 to your computer and use it in GitHub Desktop.
Save mradhi/a95e416f2194ee01038d399d5e06d7e0 to your computer and use it in GitHub Desktop.
Controller template for MateLemonBundle and MateRestBundle with NelmioApiDocBundle
<?php
/**
* This file generated automatically with MateLemonBundle Generator
* Class: <controllerClassName> Controller
* God: rg@mate.tn
*/
namespace <controllerNameSpace>;
use <entityClass>;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class <controllerClassName>
* @Route("<controllerGlobalRoute>")
*/
class <controllerClassName> extends Controller {
/**
* @ApiDoc(
* section="<entityPluralName>",
* resource="/<entityLowerPluralName>",
* resourceDescription="<entityName> Entity.",
* description="Add a new <entityLowerName>.",
* output={
* "class": "<entityClass>",
* "parsers": { "Nelmio\ApiDocBundle\Parser\JmsMetadataParser" }
* },
* statusCodes={
* 200="Returned when successful",
* 403="Returned when the user is not authorized",
* 400="Invalid input"
* },
* parameters={
* {"name"="title", "dataType"="string", "required"=true, "description"="Post title"},
* {"name"="body", "dataType"="string", "required"=false, "description"="Post body"}
* }
* )
*
* @return JsonResponse
* @throws \Exception
*
* @Route("<indexRoutePath>", name="<createRouteName>")
* @Method({"POST"})
*/
public function <createAction>()
{
$request = $this->get('mate_rest.request')->getRequest();
$<entityLowerName> = $this->get('mate_rest.system')->write(new <entityName>(), $request->all(), 'create');
return new JsonResponse($<entityLowerName>->render());
}
/**
* @ApiDoc(
* section="<entityPluralName>",
* resource="/<entityLowerPluralName>",
* resourceDescription="<entityName> Entity.",
* description="Update an existing <entityLowerName>.",
* output={
* "class": "<entityClass>",
* "parsers": { "Nelmio\ApiDocBundle\Parser\JmsMetadataParser" }
* },
* statusCodes={
* 200="Returned when successful",
* 403="Returned when the user is not authorized",
* 400="Invalid input",
* 404="Not found"
* },
* parameters={
* {"name"="title", "dataType"="string", "required"=true, "description"="Post title"},
* {"name"="body", "dataType"="string", "required"=false, "description"="Post body"}
* }
* )
*
* @param <entityName> $<entityLowerName>
*
* @return JsonResponse
* @throws \Exception
*
* @Route("/{<entityLowerName>}", name="<updateRouteName>")
* @Method({"POST"})
*/
public function <updateAction>(<entityName> $<entityLowerName>)
{
$request = $this->get('mate_rest.request')->getRequest();
$updated<entityName> = $this->get('mate_rest.system')->write($<entityLowerName>, $request->all(), 'update');
return new JsonResponse($updated<entityName>->render());
}
/**
* @ApiDoc(
* section="<entityPluralName>",
* resource="/<entityLowerPluralName>",
* resourceDescription="<entityName> Entity.",
* description="Deletes an existing <entityLowerName>",
* statusCodes={
* 200="Returned when successful",
* 403="Returned when the user is not authorized",
* 404="Not found"
* }
* )
*
* @param <entityName> $<entityLowerName>
*
* @return JsonResponse
* @throws \Exception
*
* @Route("/{<entityLowerName>}", name="<deleteRouteName>")
* @Method({"DELETE"})
*/
public function <deleteAction>(<entityName> $<entityLowerName>)
{
$this->get('mate_rest.system')->remove($<entityLowerName>);
return new JsonResponse([
'message' => sprintf('<entityName> with ID %s deleted successfully', $<entityLowerName>->getId())
]);
}
/**
* @ApiDoc(
* section="<entityPluralName>",
* resource="/<entityLowerPluralName>",
* resourceDescription="<entityName> Entity.",
* description="Fetch all <entityLowerName>",
* output={
* "class": "<entityClass>",
* "parsers": { "Nelmio\ApiDocBundle\Parser\JmsMetadataParser" }
* },
* statusCodes={
* 200="Returned when successful",
* 403="Returned when the user is not authorized"
* },
* parameters={
* {"name"="title", "dataType"="string", "required"=true, "description"="Post title"},
* {"name"="body", "dataType"="string", "required"=false, "description"="Post body"}
* }
* )
*
* @return JsonResponse
* @throws \Exception
*
* @Route("/", name="<indexRouteName>")
* @Method({"GET"})
*/
public function <indexAction>()
{
$<entityLowerPluralName> = $this->get('mate_rest.system')->findAll(<entityName>::class);
return new JsonResponse($<entityLowerPluralName>->render());
}
/**
* @ApiDoc(
* section="<entityPluralName>",
* resource="/<entityLowerPluralName>",
* resourceDescription="<entityName> Entity.",
* description="Find <entityLowerName> by ID",
* output={
* "class": "<entityClass>",
* "parsers": { "Nelmio\ApiDocBundle\Parser\JmsMetadataParser" }
* },
* statusCodes={
* 200="Returned when successful",
* 403="Returned when the user is not authorized",
* 404="Not found"
* }
* )
*
* @param <entityName> $<entityLowerName>
*
* @return JsonResponse
* @throws \Exception
*
* @Route("/{<entityLowerName>}", name="<showRouteName>")
* @Method({"GET"})
*/
public function <showAction>(<entityName> $<entityLowerName>)
{
return new JsonResponse($this->get('mate_rest.system')->render($<entityLowerName>));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment