Я реализую приложение под Symfony4 для создания API.Я ищу создать родительский контроллер для CRUD, который будет расширен на другие мои контроллеры, чтобы избежать копирования всего кода CRUD.У вас есть пример, пожалуйста?Вот что я уже сделал на контроллере.
<?php
namespace App\Controller;
use App\Entity\Article;
use App\Exception\ResourceValidationException;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Nelmio\ApiDocBundle\Annotation\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use App\Representation\Articles;
use Symfony\Component\Validator\ConstraintViolationList;
use Swagger\Annotations as SWG;
class ArticleController extends FOSRestController
{
/**
* @Rest\Get(
* path="/api/articles/{id}",
* name="app_article_show",
* requirements={"id"="\d+"}
* )
* @Rest\View()
*
* @SWG\Response(
* response=200,
* description="Return article."
* )
* @SWG\Tag(name="Article")
* @Security(name="Bearer")
*/
public function showAction(Article $article)
{
return $article;
}
/**
* @Rest\Post(
* path = "/api/articles",
* name= "app_article_create",
* )
* @Rest\View(StatusCode= 201)
* @ParamConverter(
* "article", converter="fos_rest.request_body",
* options={
"validator"={ "groups"="Create"}
* }
* )
* @SWG\Response(
* response=200,
* description="Create article"
* )
* @SWG\Tag(name="Article")
* @Security(name="Bearer")
*/
public function createAction(Article $article, ConstraintViolationList $violations)
{
if (count($violations)) {
$message = 'The JSON sent contain invalid data: ';
foreach ($violations as $violation) {
$message .= sprintf(
"Field %s: %s",
$violation->getPropertyPath(),
$violation->getMessage()
);
}
throw new ResourceValidationException($message);
}
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
return $this->view(
$article,
Response::HTTP_CREATED,
[
'Location' => $this->generateUrl('app_article_create', ['id', $article->getId()], UrlGeneratorInterface::ABSOLUTE_URL)
]
);
}
/**
* @Rest\Get(
* path="/api/articles",
* name="app_article_list"
* )
* @Rest\QueryParam(
* name="keyword",
* requirements="[a-zA-Z0-9]",
* nullable=true,
* description="The keyword to search for."
* )
* @Rest\QueryParam(
* name="order",
* requirements="asc|desc",
* default="asc",
* description="Sort order (asc or desc)"
* )
* @Rest\QueryParam(
* name="limit",
* requirements="\d+",
* default="16",
* description="Max number of movies per page."
* )
* @Rest\QueryParam(
* name="offset",
* requirements="\d+",
* default="1",
* description="The pagination offset"
* )
* @Rest\View()
* @SWG\Response(
* response=200,
* description="List all articles."
* )
* @SWG\Tag(name="Article")
* @Security(name="Bearer")
*/
public function listAction(ParamFetcherInterface $paramFetcher)
{
$pager = $this->getDoctrine()->getRepository(Article::class)->search(
$paramFetcher->get('keyword'),
$paramFetcher->get('order'),
$paramFetcher->get('limit'),
$paramFetcher->get('offset')
);
return new Articles($pager);
}
}
Спасибо за вашу помощь.