带有JMSSerialization的Symfony RestBundle setSerializationContext

时间:2017-01-05 15:57:25

标签: php symfony fosrestbundle jmsserializerbundle

"friendsofsymfony/rest-bundle": "~1.4",中,我们在{1}}中设置了来自"jms/serializer-bundle": "^1.1.0",的setSerializationContext,我们设置gropu并启用深度

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

"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;
}

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

1 个答案:

答案 0 :(得分:4)

你应该看看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();

After:

use FOS\RestBundle\Context\Context;

$view = new View();

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

$context = $view->getContext();