使用api平台实体制作博客页面

时间:2020-09-14 17:58:35

标签: symfony api-platform.com

我目前正在symfony 5中制作一个Blog页面,我认为使用api平台进行创建可能很酷。

我创建了我的实体,该实体在api页面中可以完美地执行所有操作。

    <?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\ConceptRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Carbon\Carbon;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(
 *      collectionOperations={"get", "post"},
 *      itemOperations={
 *          "get"={}, 
 *          "put",
 *          "delete",
 *          "patch",
 *          }
 *      },
 *      normalizationContext={"groups"={"concept:read"}, "swagger_definition_name"="Read"},
 *      denormalizationContext={"groups"={"concept:write"}, "swagger_definition_name"="Write"},
 *      shortName="concept",
 *      attributes={
 *          "pagination_items_per_page"=10
 *      }
 * )
 * @ORM\Entity(repositoryClass=ConceptRepository::class)
 */
class Concept
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"concept:read", "concept:write"})
     * @Assert\Length(
     *      min=2,
     *      max=255,
     *      maxMessage="Cannot exceed 255 characters"
     * )
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     * @Groups({"concept:read"})
     * @Assert\NotBlank()
     */
    private $content;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    public function __construct(string $title = null)
    {
        $this->createdAt = new \DateTimeImmutable();
        $this->title = $title;
    }
    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    /**
     * @Groups({"concept:read"})
     */
    public function getShortContent(): ?string
    {
        if (strlen($this->content)< 150){
            return $this->content;
        }

        return substr($this->content, 0, 150).'...';
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    /**
     * The content of the concept as raw text.
     * 
     * @Groups({"concept:write"})
     * @SerializedName("content")
     */
    public function setTextContent(string $content): self
    {
        $this->content = nl2br($content);

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->createdAt;
    }
    
    /**
     * How long ago in text that this concept was written.
     * 
     * @Groups({"concept:read"})
     */
    public function getCreatedAtAgo(): string
    {
        return Carbon::instance($this->getCreatedAt())->diffForHumans();
    }
}

特别是获取全部操作非常有趣,我想用这个来填充我的博客。

Picture of the get all result (also proof that I get a code 200)

此操作会生成一个JSON文件,我想在控制器中使用它,最后将所有项目显示在正确的HTML首页中。

你们有什么好的方法来制作序列化程序或在控制器中使用api平台序列化程序,并将数据扔进漂亮的html模板中?

感谢我的帮助^^

0 个答案:

没有答案