Symfony RestBundle 与 JMSSerialization setSerializationContext

shu*_*van 1 php symfony fosrestbundle jmsserializerbundle

"friendsofsymfony/rest-bundle": "~1.4",我们设置了 setSerializationContext 的"jms/serializer-bundle": "^1.1.0",上下文中,我们设置了 gropu 并启用了深度

        return View::create()
        ->setStatusCode(200)
        ->setData($certificatesResponse)
        ->setSerializationContext(
            SerializationContext::create()
                ->enableMaxDepthChecks()
                ->setGroups(array('certificates_by_parameters'))
        );
Run Code Online (Sandbox Code Playgroud)

早期"friendsofsymfony/rest-bundle": "~1.4",我们有来自 RestBundle 的 View 类的这个函数

    /**
 * Sets the serialization context.
 *
 * @param SerializationContext $serializationContext
 *
 * @return View
 */
public function setSerializationContext(SerializationContext $serializationContext)
{
    $this->serializationContext = $serializationContext;

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

"friendsofsymfony/rest-bundle": "^2.0",我没有找到这个功能,如何在2.0版本中设置序列化上下文?

sma*_*ber 5

您应该查看此处https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md

==>

View::setSerializationContext and View::getSerializationContext have been removed. Use View::setContext and View::getContext together with the new Context class instead.

Before:

use JMS\Serializer\SerializationContext;

$view = new View();

$context = new SerializationContext();
$view->setSerializationContext($context);

$context = $view->getSerializationContext();
Run Code Online (Sandbox Code Playgroud)

After:

use FOS\RestBundle\Context\Context;

$view = new View();

$context = new Context();
$view->setContext($context);

$context = $view->getContext();
Run Code Online (Sandbox Code Playgroud)