getResponseFormat example


  public function onResponse(ResponseEvent $event) {
    $response = $event->getResponse();
    if (!$response instanceof ResourceResponseInterface) {
      return;
    }

    $request = $event->getRequest();
    $format = $this->getResponseFormat($this->routeMatch, $request);
    $this->renderResponseBody($request$response$this->serializer, $format);
    $event->setResponse($this->flattenResponse($response));
  }

  /** * Determines the format to respond in. * * Respects the requested format if one is specified. However, it is common to * forget to specify a response format in case of a POST or PATCH. Rather than * simply throwing an error, we apply the robustness principle: when POSTing * or PATCHing using a certain format, you probably expect a response in that * same format. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The current route match. * @param \Symfony\Component\HttpFoundation\Request $request * The current request. * * @return string * The response format. */
$route_requirements = $this->generateRouteRequirements($supported_response_formats$supported_request_formats);

      $route_match = new RouteMatch('test', new Route('/rest/test', ['_rest_resource_config' => $this->randomMachineName()]$route_requirements));

      $resource_response_subscriber = new ResourceResponseSubscriber(
        $this->prophesize(SerializerInterface::class)->reveal(),
        $this->prophesize(RendererInterface::class)->reveal(),
        $route_match
      );

      $this->assertSame($expected_response_format$resource_response_subscriber->getResponseFormat($route_match$request));
    }
  }

  /** * @covers ::onResponse * @covers ::getResponseFormat * @covers ::renderResponseBody * @covers ::flattenResponse * * @dataProvider providerTestResponseFormat */
  
Home | Imprint | This part of the site doesn't use cookies.