2

我正在使用带有 Symfony 3.1 的 API 平台 2.0。

我按照 WIP 文档有条件地将组添加到序列化上下文中。为此,我装饰了 ContextBuilder。

这可以很好地根据当前登录的用户设置一些组。但现在我还想根据请求的资源项添加一些组 - 所以我需要将其作为对象提供,已经从持久层获取。

像这样的东西:

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 中获得请求的项目/集合。如果是这样,我真的很乐意在哪里建立我的序列化组得到一些建议。

在 EventListener 中,我可以这样做以获得我所说的“$requestedProduct”:

$subject = $event->getControllerResult()) 

非常感谢你的帮助。

干杯本

4

1 回答 1

0

它可从请求属性中获得:

$request->attributes->get('data');

https://github.com/api-platform/core/blob/2d252cfe1a188cc0d05c70a78d111b7e54c6d650/src/EventListener/ReadListener.php#L61

于 2016-09-27T13:27:54.633 回答