您好我想动态更改序列化上下文的组。
代码:
/**
* @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();
}
如代码中所述,我想在控制器中删除或添加组。 有办法吗?
答案 0 :(得分:6)
经过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);
}