CacheableAjaxResponse example

/** * Processes a request that will vary with Accept header. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request object. * * @return mixed */
  public function content(Request $request) {
    if ($request->getRequestFormat() === 'json' && $request->query->get('_wrapper_format') === 'drupal_ajax') {
      $response = new CacheableAjaxResponse(['content' => 'oh hai this is ajax']);
    }
    elseif ($request->getRequestFormat() === 'json') {
      $response = new CacheableJsonResponse(['content' => 'oh hai this is json']);
    }
    else {
      $response = new CacheableResponse("<p>oh hai this is html.</p>");
    }
    $response->addCacheableDependency((new CacheableMetadata())->addCacheContexts(['url.query_args:_wrapper_format']));
    return $response;
  }

}
Home | Imprint | This part of the site doesn't use cookies.