Php 如何使用条令和jms/serializer在Lamas/mezzio中序列化实体

Php 如何使用条令和jms/serializer在Lamas/mezzio中序列化实体,php,serialization,doctrine,mezzio,laminas,Php,Serialization,Doctrine,Mezzio,Laminas,我们目前正在使用laminas/mezzio开发一个API,我想为详细信息和列表路线生成不同的输出。我正在寻找产生以下结果的方法: 列表响应 详细答复 为此,我找到了包jms/serializer。我已经添加了序列化程序所需的所有注释,并且可以正常工作。但是我有点不知道如何将这个包集成到《条令2》和《条令2》中的lamas/mezzio和lamas/mezzio hal 我的ListHandler: <?php class ListHandler implements RequestHan

我们目前正在使用
laminas/mezzio
开发一个API,我想为
详细信息
列表
路线生成不同的输出。我正在寻找产生以下结果的方法:

列表响应 详细答复 为此,我找到了包
jms/serializer
。我已经添加了序列化程序所需的所有注释,并且可以正常工作。但是我有点不知道如何将这个包集成到《条令2》和《条令2》中的
lamas/mezzio
lamas/mezzio hal

我的ListHandler:

<?php
class ListHandler implements RequestHandlerInterface
{
    protected SnapshotServiceInterface $snapshotService;
    protected HalResponseFactory $halResponseFactory;
    protected ResourceGenerator $resourceGenerator;
    protected int $itemsPerPage;

    public function __construct(
        array $config,
        SnapshotServiceInterface $snapshotService,
        HalResponseFactory $halResponseFactory,
        ResourceGenerator $resourceGenerator
    ) {
        $this->snapshotService = $snapshotService;
        $this->halResponseFactory = $halResponseFactory;
        $this->resourceGenerator = $resourceGenerator;
    }

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $paramsQuery = $request->getQueryParams();
        $paramsRoute = $request->getAttributes();
        $params = array_merge($paramsQuery, $paramsRoute);
        $itemsPerPage = (array_key_exists('itemsPerPage', $params)) ? (int)$params['itemsPerPage'] : $this->itemsPerPage;

        $query = $this->snapshotService->getBy($params);
        $paginator = new SnapshotListCollection($query->setMaxResults($itemsPerPage));
        $resource = $this->resourceGenerator->fromObject($paginator, $request);

        return $this->halResponseFactory->createResponse($request, $resource);
    }
} 

在本例中,我看不出您集成了JMS/序列化程序。您可以创建一个value对象,该对象将为List响应和details响应提供详细信息,然后您可以要求JMS/Serializer将它们输出为JSON。您可能可以在halResponseFactory中实现它?
{
  "id": "9c81ed3e-4ec4-4c5f-a3a1-d14e499b2607",
  "name": "Lorem Ipsum 1",
  "foo": "bar",
  "more": "fields"
}
<?php
class ListHandler implements RequestHandlerInterface
{
    protected SnapshotServiceInterface $snapshotService;
    protected HalResponseFactory $halResponseFactory;
    protected ResourceGenerator $resourceGenerator;
    protected int $itemsPerPage;

    public function __construct(
        array $config,
        SnapshotServiceInterface $snapshotService,
        HalResponseFactory $halResponseFactory,
        ResourceGenerator $resourceGenerator
    ) {
        $this->snapshotService = $snapshotService;
        $this->halResponseFactory = $halResponseFactory;
        $this->resourceGenerator = $resourceGenerator;
    }

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $paramsQuery = $request->getQueryParams();
        $paramsRoute = $request->getAttributes();
        $params = array_merge($paramsQuery, $paramsRoute);
        $itemsPerPage = (array_key_exists('itemsPerPage', $params)) ? (int)$params['itemsPerPage'] : $this->itemsPerPage;

        $query = $this->snapshotService->getBy($params);
        $paginator = new SnapshotListCollection($query->setMaxResults($itemsPerPage));
        $resource = $this->resourceGenerator->fromObject($paginator, $request);

        return $this->halResponseFactory->createResponse($request, $resource);
    }
}