1

您好,我想动态更改我的序列化上下文的组。

编码 :

/**
 * @Rest\Get("", name="bap_api_space_query")
 * @Rest\View(serializerGroups={"Default", "space_dashboard", "dashboard_resource"})
 *
 * @ApiDoc(resource=true,description="List all spaces this user has access to")
 */
public function queryAction(Request $request)
{
    $user = $this->getUser()->reload();
    $organization = $user->getOrganization();

    // depending the request, remove or add serialized group
    // for example $view->setSerializationGroups('dashboard');

    return $organization->getSpaces();
}

如代码中所述,我想在控制器中删除或添加组。有没有办法做到这一点?

4

1 回答 1

8

经过 5 小时的研究,解决方案非常简单:

public function queryAction(Request $request)
{

    $user = $this->getUser()->reload();
    $organization = $user->getOrganization();

    // filter spaces where org has an active contract

    $context = new Context();
    $context->setGroups(array('Default'));
    $spaces = $organization->getSpaces();
    $view = $this->view($spaces, 200);
    $view->setContext($context);


    return $this->handleView($view);
}
于 2016-08-11T13:43:40.937 回答