Domanda

My Symfony2 API uses FOSRestBundle and JMSSerializer, with property annotations, but there are many times when I don't want to expose every property. I understand JMS has exclusion groups, but I can't figure out how to include those in my Symfony controllers. There should be a way to use PHP on a dynamic basis but that seems to be missing from the documentation too.

È stato utile?

Soluzione

If you use View class like in this example, you can set serialization context with setSerializationContext method

public function getUsersAction()
{
    $data = // get data, in this case list of users.
    $view = $this->view($data, 200)
        ->setSerializationContext(SerializationContext::create()->setGroups(array('list')))
    ;

    return $this->handleView($view);
}

Altri suggerimenti

Since FOSRest 2.0 version you must use this:

$view = $this->view($response, $code);
$view->setContext($view->getContext()->setGroups(['get_client']));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top