【问题标题】:Zend Expressive API does not return contents of objectsZend Expressive API 不返回对象的内容
【发布时间】:2019-03-14 23:21:37
【问题描述】:

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

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 响应,其中包含 18 个“产品”实体的列表。我的结果看起来像。

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

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

使用 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;
    }
}

【问题讨论】:

  • 能否展示产品实体类的代码?它是否从 zend 扩展了一个类?
  • @Don'tPanic 代码已添加。我不扩展任何课程。

标签: php json api mezzio


【解决方案1】:

您需要公开属性,或者在您的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() 方法,该方法以json_encode 可访问的格式在JSON 结果中返回您想要的属性(具有公共属性的对象或数组)。

【讨论】:

  • 太棒了,谢谢!我有一种感觉是问题所在,但不太确定!我会尽快尝试并接受答案!
  • 当我从数据库查询中设置 $data 变量时,我还有什么需要做的吗?
  • 我不这么认为。我相信 $data 将只是 Product 对象的数组,这些对象将由 JsonResponse 类编码。
猜你喜欢
  • 2021-11-14
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2013-05-05
  • 1970-01-01
  • 2013-02-21
相关资源
最近更新 更多