Created
May 17, 2016 09:10
-
-
Save taiebb/8c6a74e5c9074356cf2649937692f177 to your computer and use it in GitHub Desktop.
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 AppBundle\Controller\Api; | |
header('Access-Control-Allow-Origin: *'); | |
use FOS\RestBundle\Controller\FOSRestController; | |
use FOS\RestBundle\Controller\Annotations as Rest; | |
use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
/** | |
* Api Article controller. | |
*/ | |
class ApiArticleController extends FOSRestController implements ApiInterface { | |
/** | |
* Lister les articles | |
* | |
* @ApiDoc( | |
* resource = true, | |
* description = "Lister les articles", | |
* section = "Articles", | |
* statusCodes = { | |
* 200 = "Requête traitée avec succès", | |
* 404 = "Ressource non trouvée" | |
* } | |
* ) | |
* @Rest\Get("api/articles", name="api_articles", options={ "method_prefix" = false }) | |
*/ | |
public function indexAction() { | |
$apiManager = $this->get('article_api_manager'); | |
return $apiManager->processIndex(); | |
} | |
/** | |
* Ajouter un article | |
* | |
* @ApiDoc( | |
* resource = true, | |
* description = "Ajouter un article", | |
* section = "Articles", | |
* statusCodes = { | |
* 200 = "Requête traitée avec succès", | |
* 404 = "Ressource non trouvée" | |
* } | |
* ) | |
* @Rest\Post("api/articles", name="api_new_article", | |
* options={ "method_prefix" = false }) | |
*/ | |
public function newAction() { | |
$apiManager = $this->get('article_api_manager'); | |
return $apiManager->processCreateForm(); | |
} | |
/** | |
* Afficher un article selon son identifiant (id) | |
* | |
* @ApiDoc( | |
* resource = true, | |
* description = "Afficher un article", | |
* section = "Articles", | |
* requirements={ | |
* { | |
* "name"="id", | |
* "dataType"="integer", | |
* "requirement"="true", | |
* "description"="Identifiant de l'article", | |
* } | |
* }, | |
* statusCodes = { | |
* 200 = "Requête traitée avec succès", | |
* 404 = "Ressource non trouvée" | |
* } | |
* ) | |
* @Rest\Get("api/articles/{id}", name="api_article", | |
* requirements={"id" = "\d+"}, options={ "method_prefix" = false }) | |
*/ | |
public function showAction($id) { | |
$apiManager = $this->get('article_api_manager'); | |
return $apiManager->processShow($id); | |
} | |
/** | |
* Modifier un article | |
* | |
* @ApiDoc( | |
* resource = true, | |
* description = "Modifier un article", | |
* section = "Articles", | |
* requirements={ | |
* { | |
* "name"="id", | |
* "dataType"="integer", | |
* "requirement"="true", | |
* "description"="Identifiant de l'article", | |
* } | |
* }, | |
* statusCodes = { | |
* 200 = "Requête traitée avec succès", | |
* 404 = "Ressource non trouvée" | |
* } | |
* ) | |
* @Rest\Put("api/articles/{id}", name="api_update_article", | |
* options={ "method_prefix" = false }) | |
*/ | |
public function editAction($id) { | |
$apiManager = $this->get('article_api_manager'); | |
return $apiManager->processEditForm($id); | |
} | |
/** | |
* Supprimer un article | |
* | |
* @ApiDoc( | |
* resource = true, | |
* description = "Supprimer un article", | |
* section = "Articles", | |
* requirements={ | |
* { | |
* "name"="id", | |
* "dataType"="integer", | |
* "requirement"="true", | |
* "description"="Identifiant de l'article", | |
* } | |
* }, | |
* statusCodes = { | |
* 200 = "Requête traitée avec succès", | |
* 404 = "Ressource non trouvée" | |
* } | |
* ) | |
* @Rest\Delete("api/articles/{id}", name="api_delete_article", | |
* requirements={"id" = "\d+"}, options={ "method_prefix" = false }) | |
*/ | |
public function deleteAction($id) { | |
$apiManager = $this->get('article_api_manager'); | |
return $apiManager->processDelete($id); | |
} | |
} |
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 AppBundle\Services; | |
use Doctrine\ORM\EntityManager; | |
use FOS\RestBundle\View\View; | |
use AppBundle\Entity\Article; | |
use FOS\RestBundle\View\ViewHandler; | |
use Symfony\Bundle\FrameworkBundle\Routing\Router; | |
use Symfony\Component\Form\Form; | |
use Symfony\Component\HttpFoundation\Request; | |
class ArticleApiManager { | |
private $request; | |
private $em; | |
private $repository; | |
private $viewHandler; | |
private $router; | |
private $form; | |
public function __construct(Request $request, EntityManager $em | |
, ViewHandler $viewHandler, Router $router, Form $Form) { | |
$this->request = $request; | |
$this->em = $em; | |
$this->viewHandler = $viewHandler; | |
$this->router = $router; | |
$this->form = $Form; | |
$this->repository = $em->getRepository('AppBundle:Article'); | |
} | |
public function setRepository($repository) { | |
$this->repository = $repository; | |
} | |
public function processIndex() { | |
$articles = $this->repository->findAll(); | |
return View::create($articles, '200'); | |
} | |
public function processShow($id) { | |
$article = $this->repository->find($id); | |
if (!$article instanceof Article) { | |
$data = array('documentaton' => $this->router->generate('nelmio_api_doc_index'), | |
'errorCode' => '404', 'message' => 'Article non trouvé!'); | |
return View::create($data, '404'); | |
} | |
return View::create($article, '200'); | |
} | |
public function processDelete($id) { | |
$article = $this->repository->find($id); | |
if (!$article instanceof Article) { | |
$data = array('documentaton' => $this->generateUrl('nelmio_api_doc_index'), | |
'errorCode' => '404', 'message' => 'Article non trouvé!'); | |
return View::create($data, '404'); | |
} | |
$this->em->remove($article); | |
$this->em->flush(); | |
return View::create(NULL, '204'); | |
} | |
public function processCreateForm() { | |
$this->form->handleRequest($this->request); | |
// check if $article is a new object or existant one | |
// $statusCode = $this->em->contains($this->getForm()->getData()) ? 204 : 201; | |
if ($this->form->isValid()) { | |
$this->em->persist($this->getForm()->getData()); | |
$this->em->flush(); | |
$view = new View(); | |
$view->setStatusCode('201'); | |
// set the `Location` header only when creating new resources | |
$url = $this->router->generate( | |
'api_article', array('id' => $this->getForm()->getData()->getId()), true | |
); | |
$view->setHeader('Location', $url); | |
return $this->viewHandler->handle($view); | |
} | |
return View::create($this->form, 400); | |
} | |
public function processEditForm($id) { | |
$article = $this->repository->find($id); | |
if (!$article instanceof Article) { | |
$data = array('documentaton' => $this->router->generate('nelmio_api_doc_index'), | |
'errorCode' => '404', 'message' => 'Article non trouvé!'); | |
return View::create($data, '404'); | |
} | |
$this->form->setData($article); | |
$this->form->handleRequest($this->request); | |
$this->form->submit($this->request); | |
if ($this->form->isValid()) { | |
$this->em->persist($this->getForm()->getData()); | |
$this->em->flush(); | |
return View::create(null, '204'); | |
} | |
return View::create($this->form, 400); | |
} | |
public function getForm() { | |
return $this->form; | |
} | |
} |
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
# Learn more about services, parameters and containers at | |
# http://symfony.com/doc/current/book/service_container.html | |
parameters: | |
format: 'json' | |
services: | |
app_deserialize: | |
class: AppBundle\Services\Deserialize | |
arguments: ["%format%", "@request_stack", "@jms_serializer", "@validator"] | |
scope: request | |
article_form: | |
factory_service: form.factory | |
factory_method: createNamed | |
class: Symfony\Component\Form\Form | |
arguments: | |
- article | |
- article_form | |
article_type: | |
class: AppBundle\Form\ArticleType | |
tags: | |
- { name: form.type, alias: article_form } | |
article_handler: | |
class: AppBundle\Form\Handler\ArticleFormHandler | |
arguments: [@article_form, @request] | |
scope: request | |
article_api_manager: | |
class: AppBundle\Services\ArticleApiManager | |
arguments: ["@request", "@doctrine.orm.entity_manager", "@fos_rest.view_handler", '@router', @article_form] | |
scope: request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment