我正在使用 PhlyRestfully 开发一个基于 Web 的 API,该 API 在其他模块中具有业务逻辑(服务、实体...)。为此,我创建了一个名为“Api”的模块,该模块已将 PhlyRestfully 配置为使用 hydrator ClassMethods
。问题是我的实体有很多礼仪,而 api 响应反映了实体,我不希望这样。
例如,我的实体具有以下特性:
class Deal {
$deal_id;
$title;
$description;
$price;
}
在 Model/Service 中创建的 fetch 方法返回一个 Deal 但它只选择 title 和 deal_id 因为这些只是我想在响应中显示的列。响应不仅包含我想要的 title 和 deal_id,而且反映了 hole 实体。如果我没有在配置中指定水合器,则响应是我想要的(带有标题和 deal_id),但响应中还有另一个问题,密钥有一个附加文本(“\u0000*\u0000)。从我看到的在代码中,由于演员表而添加了附加文本,并且需要水合器。
有什么解决方案可以解决这个问题,而无需在“Api”模块中构建单独的逻辑?有人可以建议我任何其他解决方案吗?
资源:
namespace Api\Resources;
class Deals extends AbstractListenerAggregate
{
protected $listeners = array();
protected $table;
public function __construct($table)
{
$this->table = $table;
}
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach('create', array($this, 'onCreate'));
$this->listeners[] = $events->attach('fetch', array($this, 'onFetch'));
$this->listeners[] = $events->attach('fetchAll', array($this, 'onFetchAll'));
}
public function onCreate(ResourceEvent $e)
{
$data = $e->getParam('data');
$paste = $this->table->save($data);
if (!$paste) {
throw new CreationException();
}
return $paste;
}
public function onFetch(ResourceEvent $e)
{
$id = $e->getParam('id');
$paste = $this->table->getDealMapper()->fetchById($id);
$paste->addresses = new HalResource(array('deals'), 1);
if (!$paste) {
throw new DomainException('Paste not found', 404);
}
return $paste;
}
public function onFetchAll(ResourceEvent $e)
{
// getDealsApi is selecting only the deal_id and the title
$deals = $this->table->getDealMapper()->getDealsApi();
foreach($deals as $deal) {
$deal->categories = array(
'3' => array(
'name' => 'asdasd',
'href' => 'url'
)
);
}
return $deals;
}
}
来自“通用”模块的实体:
namespace Common\Entity;
class Deal
{
protected $deal_id;
protected $store_id;
protected $title;
protected $short_description;
protected $list_price;
protected $price;
protected $discount;
protected $discount_type;
protected $image_url;
protected $link;
/**
* @return dealId
*/
public function getDealId()
{
return $this->deal_id;
}
/**
* @param $dealId
* @return self
*/
public function setDealId($dealId)
{
$this->deal_id = (int) $dealId;
return $this;
}
/**
* @return store_id
*/
public function getStoreId()
{
return $this->store_id;
}
/**
* @param $storeId
* @return self
*/
public function setStoreId($storeId)
{
$this->store_id = $storeId;
return $this;
}
/**
* @return title
*/
public function getTitle()
{
return $this->title;
}
/**
* @param $title
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* @return shortDescription
*/
public function getShortDescription()
{
return $this->short_description;
}
/**
* @param $description
* @return self
*/
public function setShortDescription($description)
{
$this->short_description = $description;
return $this;
}
/**
* @return link
*/
public function getLink()
{
return $this->link;
}
/**
* @param $link
* @return self
*/
public function setLink($link)
{
$this->link = $link;
return $this;
}
/**
* @return list_price
*/
public function getListPrice()
{
return $this->list_price;
}
/**
* @param $listPrice
* @return self
*/
public function setListPrice($listPrice)
{
$this->list_price = $listPrice;
return $this;
}
/**
* @return price
*/
public function getPrice()
{
return $this->price;
}
/**
* @param $price
* @return self
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* @return discount
*/
public function getDiscount()
{
return $this->discount;
}
/**
* @param $discount
* @return self
*/
public function setDiscount($discount)
{
$this->discount = $discount;
return $this;
}
/**
* @return discount_type
*/
public function getDiscountType()
{
return $this->discount_type;
}
/**
* @param $discounType
* @return self
*/
public function setDiscountType($discounType)
{
$this->discount_type = $discounType;
return $this;
}
/**
* @return image_url
*/
public function getImageUrl()
{
return $this->image_url;
}
/**
* @param $imageUrl
* @return self
*/
public function setImageUrl($imageUrl)
{
$this->image_url = $imageUrl;
return $this;
}
}
API 模块配置:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Api\Controller\Index' => 'Api\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'api' => array(
'type' => 'Literal',
'options' => array(
'route' => '/api',
'defaults' => array(
'__NAMESPACE__' => 'Api\Controller',
'controller' => 'Index',
// 'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'deals' => array(
'type' => 'Segment',
'options' => array(
'route' => '/deals[/[:id]]',
// 'controller' => 'Api\Controller\Deals',
'defaults' => array(
'controller' => 'Api\Controller\Deals',
),
),
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy'
),
),
'phlyrestfully' => array(
'resources' => array(
'Api\Controller\Deals' => array(
'listener' => 'Api\Resource\Deal',
'route_name' => 'api/deals',
'resource_http_options' => array('get'),
'page_size' => 1,
)
),
'renderer' => array(
'hydrators' => array(
'Common\Entity\Deal' =>'Hydrator\ClassMethods',
),
),
),
'service_manager' => array(
'invokables' => array(
'Hydrator\ClassMethods' => 'Zend\Stdlib\Hydrator\ClassMethods',
),
),
);
使用此代码的响应:
{
"_links": {
"self": {
"href": "http://deal.local.com/api/deals?page=1"
},
"first": {
"href": "http://deal.local.com/api/deals"
},
"last": {
"href": "http://deal.local.com/api/deals?page=1"
}
},
"_embedded": {
"items": [
{
"deal_id": 1,
"type": null,
"store_id": null,
"title": "Title of the deal",
"short_description": null,
"long_description": null,
"status": null,
"slug": null,
"link": null,
"list_price": null,
"price": null,
"discount": null,
"discount_type": null,
"image_url": null,
}
]
}
}
从您可以看到它为 getDealsApi() 未返回的键设置 NULL
我想看起来像这样:
{
"_links": {
"self": {
"href": "http://deal.local.com/api/deals?page=1"
},
"first": {
"href": "http://deal.local.com/api/deals"
},
"last": {
"href": "http://deal.local.com/api/deals?page=1"
}
},
"_embedded": {
"items": [
{
"deal_id": 1,
"title": "Title of the deal",
}
]
}
}
任何帮助将不胜感激!
谢谢