I made this for GET statement in the controller
<?php
namespace App\Controller\Api;
use App\Entity\Article;
use App\Factory\EntityFactory;
use App\Repository\ArticleRepository;
use Swagger\Annotations as SWG;
use Nelmio\ApiDocBundle\Annotation\Model;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\Annotation\Route;
class ArticlesController
{
private $repository;
private $factory;
private $serializer;
public function __construct(ArticleRepository $repository, EntityFactory $entityFactory, SerializerInterface $serializer)
{
$this->repository = $repository;
$this->factory = $entityFactory;
$this->serializer = $serializer;
}
/**
* @Route(path="/articles/{article}", methods={"GET"}, name="article_get")
*
* @SWG\Get(
* tags={"Articles"},
* @SWG\Parameter(
* name="article",
* in="path",
* type="integer",
* description="Article ID",
* ),
* )
* @SWG\Response(
* response=200,
* description="Article fetched",
* @Model(type=Article::class, groups={"article:get", "article:category", "category:index"})
* )
*/
public function get(Article $article): JsonResponse
{
$data = $this->serializer->serialize($article, 'json', ['groups' => ['article:get', 'article:category', 'category:index']]);
return new JsonResponse($data, JsonResponse::HTTP_OK, [], true);
}
}
The entity from the article looks like this:
<?php
namespace App\Entity;
use App\Entity\User\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Swagger\Annotations as SWG;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
* @ORM\Table(name="articles")
* @ORM\HasLifecycleCallbacks
*/
class Article implements EntityInterface
{
/**
* @var int
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"article:id", "article:get", "article:index"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"article:category"})
*
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"article:create", "article:get", "article:index", "article:update"})
*/
private $lead;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"article:create", "article:get", "article:index", "article:update"})
*/
private $slug;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"article:create", "article:get", "article:index", "article:update"})
*/
private $content;
/**
* @ORM\Column(type="datetime")
* @Groups({"article:create", "article:get", "article:index", "article:update"})
* @SWG\Property(property="updated_at")
*/
private $publishedAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="articles")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
* @Groups({"article:category"})
*
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User\User", inversedBy="articles")
* @ORM\JoinColumn(nullable=false)
* @Groups({"article:user"})
*/
private $author;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $articleFilename;
/**
* @ORM\Column(type="json_array", nullable=true)
*/
private $marking;
/**
* @ORM\Column(type="json_array", nullable=true)
*/
private $transitionContexts;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getLead(): ?string
{
return $this->lead;
}
public function setLead(string $lead): self
{
$this->lead = $lead;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getPublishedAt(): ?\DateTimeInterface
{
return $this->publishedAt;
}
public function setPublishedAt(\DateTimeInterface $publishedAt): self
{
$this->publishedAt = $publishedAt;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
public function getArticleFilename(): ?string
{
return $this->articleFilename;
}
public function setArticleFilename(?string $articleFilename): self
{
$this->articleFilename = $articleFilename;
return $this;
}
public function getImagePath()
{
return 'images/'.$this->getImageFilename();
}
public function getMarking()
{
return $this->marking;
}
public function setMarking($marking, $context = [])
{
$this->marking = $marking;
$this->transitionContexts[] = [
'new_marking' => $marking,
'context' => $context,
'time' => (new \DateTime())->format('c'),
];
}
public function getTransitionContexts()
{
return $this->transitionContexts;
}
public function setTransitionContexts($transitionContexts): self
{
$this->transitionContexts = $transitionContexts;
}
}
And when I go to http://localhost/docs I become this error. That my annotations are not enabled or installed. Thank you for your help.
Error message:
Exception thrown when handling an exception (Symfony\Component\Config\Exception\LoaderLoadException: [Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_CLOSE_CURLY_BRACES, got 'article' at position 179 in method App\Controller\Api\ArticlesController::get() in /app/config/routes/../../src/Controller/Api (which is being imported from "/app/config/routes/annotations.yaml"). Make sure annotations are installed and enabled.)