我在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);
}
}
感谢您的帮助。
答案 0 :(得分:0)
在Symfony 4之前,您可以使用Doctrine CRUD controller generation feature。如果您只是想要一个例子,请看看。
现在您可以使用Symfony Maker Bundle作为新选择。他们说here有一个新命令 make:crud ,他们改进了现有的 make:form