2
我正在使用API Platform 2.0和Symfony 3.1。Api Platform:根據請求的資源項將條件添加到序列化上下文中
我遵循WIP文檔,有條件地將組添加到序列化上下文中。爲此我裝飾了ContextBuilder。
這很好地根據當前登錄的用戶設置一些組。 但是現在我還想根據請求的資源項目添加一些組 - 所以我需要將它作爲Object從持久層中獲取。
事情是這樣的:
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null) : array {
/* @var $currentUser User */
$currentUser = $this->tokenStorage->getToken()->getUser();
/* @var $requestedProduct Product */
$requestedProduct = $this->getRequestedItem();
if ($product->getAuthoringOrganization() === $currentUser->getOrganization() {
$context['groups'][] = 'api_products_get_item_current_user_is_owner';
}
return $context;
}
恐怕我不能得到所要求的項目/集合中,ContextBuilder。如果是這樣,我會很樂意得到一些建議,建立我的序列化組。
在事件監聽我能做到這一點得到我所謂的「$ requestedProduct」在這裏:
$subject = $event->getControllerResult())
非常感謝你的幫助。
乾杯本
非常感謝你,現在我想完成我的問題,看到你已經添加了評論從拉請求討論。非常感謝你的幫助! – Benjamin