Zend Expressive API 没有 return 对象的内容

Zend Expressive API does not return contents of objects

我正在创建一个小的 API,主要用于学习目的,但是,我可能会在我正在从事的项目中实施它。到目前为止,我已经安装了 zend expressive skeleton 应用程序并设置了我的模型和实体。我能够查询数据库并获得结果,但是,当我 return 结果作为 JSON 响应时,我只能看到每个结果的空数组列表。我希望能够 return 从数据库中 return 编辑的实际对象,而不是将它们转换为数组。

HomePageHandler.php

<?php

declare(strict_types=1);

namespace App\Handler;

use App\Entity\Product;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Router;
use Zend\Expressive\Template\TemplateRendererInterface;
use App\Model\ProductModel;

class HomePageHandler implements RequestHandlerInterface
{
    /** @var string */
    private $containerName;

    /** @var Router\RouterInterface */
    private $router;

    /** @var null|TemplateRendererInterface */
    private $template;

    private $productModel;

    public function __construct(
        string $containerName,
        Router\RouterInterface $router,
        ?TemplateRendererInterface $template = null,
        ProductModel $productModel
    ) {
        $this->containerName = $containerName;
        $this->router        = $router;
        $this->template      = $template;
        $this->productModel  = $productModel;
    }

    public function handle(ServerRequestInterface $request) : ResponseInterface
    {
        $data = $this->productModel->fetchAllProducts();

        return new JsonResponse([$data]);

        //return new HtmlResponse($this->template->render('app::home-page', $data));
    }
}

我期待 JSON 响应 return 包含 18 个 "Product" 实体的列表。我的结果看起来像。

[[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]]

如果您还想查看任何其他代码,请告诉我。 提前致谢!

使用 Product.php 代码编辑

<?php
/**
 * Created by PhpStorm.
 * User: Brock H. Caldwell
 * Date: 3/14/2019
 * Time: 4:04 PM
 */

namespace App\Entity;

class Product
{
    protected $id;
    protected $picture;
    protected $shortDescription;
    protected $longDescription;
    protected $dimensions;
    protected $price;
    protected $sold;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getPicture()
    {
        return $this->picture;
    }

    /**
     * @param mixed $picture
     */
    public function setPicture($picture)
    {
        $this->picture = $picture;
    }

    /**
     * @return mixed
     */
    public function getShortDescription()
    {
        return $this->shortDescription;
    }

    /**
     * @param mixed $shortDescription
     */
    public function setShortDescription($shortDescription)
    {
        $this->shortDescription = $shortDescription;
    }

    /**
     * @return mixed
     */
    public function getLongDescription()
    {
        return $this->longDescription;
    }

    /**
     * @param mixed $longDescription
     */
    public function setLongDescription($longDescription)
    {
        $this->longDescription = $longDescription;
    }

    /**
     * @return mixed
     */
    public function getDimensions()
    {
        return $this->dimensions;
    }

    /**
     * @param mixed $dimensions
     */
    public function setDimensions($dimensions)
    {
        $this->dimensions = $dimensions;
    }

    /**
     * @return mixed
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * @param mixed $price
     */
    public function setPrice($price)
    {
        $this->price = $price;
    }

    /**
     * @return mixed
     */
    public function getSold()
    {
        return $this->sold;
    }

    /**
     * @param mixed $sold
     */
    public function setSold($sold)
    {
        $this->sold = $sold;
    }

    public function exchangeArray($data)
    {
        $this->id = (!empty($data['id'])) ? $data['id'] : null;
        $this->picture = (!empty($data['picture'])) ? $data['picture'] : null;
        $this->shortDescription = (!empty($data['shortDescription'])) ? $data['shortDescription'] : null;
        $this->longDescription = (!empty($data['longDescription'])) ? $data['longDescription'] : null;
        $this->dimensions = (!empty($data['dimensions'])) ? $data['dimensions'] : null;
        $this->price = (!empty($data['price'])) ? $data['price'] : null;
        $this->sold = (!empty($data['sold'])) ? $data['sold'] : null;
    }
}

您需要创建属性 public,或者在 Product 实体中实现 JsonSerializable 接口。它的所有属性都受到保护,这很好,但这意味着当对象被 JSON 编码时它们不会暴露。

这里有一些简单的例子:

class Example1 {  protected $a = 1;  }

class Example2 {  public $b = 2; }

class Example3 implements JsonSerializable {
    protected $c = 3;
    public function jsonSerialize() {
        return ['c' => $this->c];
    }
}

echo json_encode([new Example1, new Example2, new Example3]);

结果:

[{},{"b":2},{"c":3}]

如果您选择实施 JsonSerializable,具体如何实施取决于您,但您只需要一个 jsonSerialize() 方法,该方法 returns 您想要的属性 JSON 导致 json_encode 可访问的格式(具有 public 属性或数组的对象)。