本文整理汇总了PHP中FOS\RestBundle\View\View::setFormat方法的典型用法代码示例。如果您正苦于以下问题:PHP View::setFormat方法的具体用法?PHP View::setFormat怎么用?PHP View::setFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FOS\RestBundle\View\View
的用法示例。
在下文中一共展示了View::setFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testSetFormat
public function testSetFormat()
{
$view = new View();
$format = 'bar';
$view->setFormat($format);
$this->assertEquals($format, $view->getFormat());
}
示例2: indexAction
/**
* @Route("/")
*/
public function indexAction()
{
$view = new View();
$view->setFormat('html');
$view->setTemplate('TastdCoreBundle:Default:index.html.twig');
return $view;
}
示例3: showAction
public function showAction(Request $request, $exception, DebugLoggerInterface $logger = null)
{
$view = new View();
$view->setData(json_decode($exception->getMessage(), true));
$view->setFormat('json');
return $this->container->get('fos_rest.view_handler')->handle($view);
}
示例4: createView
protected function createView($returnData)
{
$view = new View();
$view->setData($returnData);
$view->setStatusCode($returnData['status']);
$view->setFormat('json');
return $view;
}
示例5: editSeccionAction
public function editSeccionAction($id)
{
$em = $this->getDoctrine()->getManager();
$seccion = $em->getRepository('EtsiSiteBundle:Seccion')->find($id);
$data = $this->getForm($seccion);
$view = new View($data);
$view->setTemplate('EtsiSiteBundle:Form:newSeccion.html.twig');
$view->setFormat('html');
return $this->get('fos_rest.view_handler')->handle($view);
}
示例6: editContenidoAction
public function editContenidoAction($id)
{
$em = $this->getDoctrine()->getManager();
$contenido = $em->getRepository('EtsiSiteBundle:Contenido')->find($id);
$data = $this->getForm($contenido);
$view = new View($data);
$view->setTemplate('EtsiSiteBundle:Form:newContenido.html.twig');
$view->setFormat('html');
return $this->get('fos_rest.view_handler')->handle($view);
}
示例7: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
if (!$view instanceof View) {
if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return parent::onKernelView($event);
}
$view = new View($view);
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
$view->setStatusCode($configuration->getStatusCode());
}
if ($configuration->getSerializerGroups()) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->setGroups($configuration->getSerializerGroups());
$view->setSerializationContext($context);
}
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars) {
$vars = $request->attributes->get('_template_default_vars');
}
$viewHandler = $this->container->get('fos_rest.view_handler');
if ($viewHandler->isFormatTemplating($view->getFormat())) {
if (!empty($vars)) {
$parameters = (array) $viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = $request->attributes->get('_template');
if ($template) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例8: testGetCallbackFailure
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
* @dataProvider getCallbackFailureDataProvider
*/
public function testGetCallbackFailure(Request $request)
{
$data = ['foo' => 'bar'];
$viewHandler = new ViewHandler($this->router, $this->serializer, $this->templating, $this->requestStack, $this->exceptionWrapperHandler, ['jsonp' => false]);
$jsonpHandler = new JsonpHandler('callback');
$viewHandler->registerHandler('jsonp', [$jsonpHandler, 'createResponse']);
$this->serializer->expects($this->once())->method('serialize')->will($this->returnValue(var_export($data, true)));
$data = ['foo' => 'bar'];
$view = new View($data);
$view->setFormat('jsonp');
$viewHandler->handle($view, $request);
}
示例9: testGetCallbackFailure
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
* @dataProvider getCallbackFailureDataProvider
*/
public function testGetCallbackFailure(Request $request)
{
$data = array('foo' => 'bar');
$viewHandler = new ViewHandler(array('jsonp' => false));
$jsonpHandler = new JsonpHandler('callback');
$viewHandler->registerHandler('jsonp', array($jsonpHandler, 'createResponse'));
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', array('get', 'getParameter'));
$serializer = $this->getMock('stdClass', array('serialize', 'setVersion'));
$serializer->expects($this->once())->method('serialize')->will($this->returnValue(var_export($data, true)));
$container->expects($this->once())->method('get')->with('fos_rest.serializer')->will($this->returnValue($serializer));
$container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('version', '1.0'));
$viewHandler->setContainer($container);
$data = array('foo' => 'bar');
$view = new View($data);
$view->setFormat('jsonp');
$viewHandler->handle($view, $request);
}
示例10: testGetCallbackFailure
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
* @dataProvider getCallbackFailureDataProvider
*/
public function testGetCallbackFailure(Request $request)
{
$data = ['foo' => 'bar'];
$viewHandler = new ViewHandler(['jsonp' => false]);
$jsonpHandler = new JsonpHandler('callback');
$viewHandler->registerHandler('jsonp', [$jsonpHandler, 'createResponse']);
$viewHandler->setSerializationContextAdapter($this->getMock(SerializationContextAdapterInterface::class));
$container = $this->getMock(ContainerInterface::class);
$serializer = $this->getMock('stdClass', ['serialize', 'setVersion']);
$serializer->expects($this->once())->method('serialize')->will($this->returnValue(var_export($data, true)));
$container->expects($this->any())->method('get')->with($this->equalTo('fos_rest.serializer'))->will($this->returnValue($serializer));
$container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('version', '1.0'));
$viewHandler->setContainer($container);
$data = ['foo' => 'bar'];
$view = new View($data);
$view->setFormat('jsonp');
$viewHandler->handle($view, $request);
}
示例11: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$view = $event->getControllerResult();
if (!$view instanceof View) {
if (!$request->attributes->get('_view') && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return;
}
$view = new View($view);
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars) {
$vars = $request->attributes->get('_template_default_vars');
}
if (!empty($vars)) {
$parameters = $view->getData();
if (null !== $parameters && !is_array($parameters)) {
throw new \RuntimeException('View data must be an array if using a templating aware format.');
}
$parameters = (array) $parameters;
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$viewHandler = $this->container->get('fos_rest.view_handler');
if ($viewHandler->isFormatTemplating($view->getFormat())) {
$template = $request->attributes->get('_template');
if ($template) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
$template->set('engine', null);
}
$view->setTemplate($template);
}
}
$response = $viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例12: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
$customViewDefined = true;
if (!$view instanceof View) {
if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return parent::onKernelView($event);
}
$view = new View($view);
$customViewDefined = false;
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Codes::HTTP_OK === $view->getStatusCode())) {
$view->setStatusCode($configuration->getStatusCode());
}
if ($configuration->getSerializerGroups() && !$customViewDefined) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->setGroups($configuration->getSerializerGroups());
$view->setSerializationContext($context);
}
if ($configuration->getSerializerEnableMaxDepthChecks()) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->enableMaxDepthChecks();
$view->setSerializationContext($context);
}
$populateDefaultVars = $configuration->isPopulateDefaultVars();
} else {
$populateDefaultVars = true;
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars && $populateDefaultVars) {
$vars = $request->attributes->get('_template_default_vars');
}
$viewHandler = $this->container->get('fos_rest.view_handler');
if ($viewHandler->isFormatTemplating($view->getFormat())) {
if (!empty($vars)) {
$parameters = (array) $viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = $request->attributes->get('_template');
if ($template) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例13: testPrepareTemplateParametersWithProvider
/**
* @dataProvider prepareTemplateParametersDataProvider
*/
public function testPrepareTemplateParametersWithProvider($viewData, $templateData, $expected)
{
$handler = new ViewHandler(array('html' => true));
$view = new View();
$view->setFormat('html');
$view->setData($viewData);
if (null !== $templateData) {
$view->setTemplateData($templateData);
}
$this->assertEquals($expected, $handler->prepareTemplateParameters($view));
}
示例14: testPrepareTemplateParametersWithProvider
/**
* @dataProvider prepareTemplateParametersDataProvider
*/
public function testPrepareTemplateParametersWithProvider($viewData, $templateData, $expected)
{
$handler = $this->createViewHandler(['html' => true]);
$handler->setSerializationContextAdapter($this->getMock('FOS\\RestBundle\\Context\\Adapter\\SerializationContextAdapterInterface'));
$view = new View();
$view->setFormat('html');
$view->setData($viewData);
if (null !== $templateData) {
$view->setTemplateData($templateData);
}
$this->assertEquals($expected, $handler->prepareTemplateParameters($view));
}
示例15: jsonpAction
public function jsonpAction()
{
$data = array('foo' => 'bar');
$view = new View($data);
$view->setFormat('jsonp');
return $this->viewHandler->handle($view);
}