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

问题描述

我目前正在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,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模板中?

感谢我的帮助^^

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)