You are here

public function XmlSitemapController::getSitemapResponse in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 src/Controller/XmlSitemapController.php \Drupal\xmlsitemap\Controller\XmlSitemapController::getSitemapResponse()

Creates a response object that will output the sitemap file.

Parameters

string $file: File uri.

\Symfony\Component\HttpFoundation\Request $request: The current request.

array $headers: An array of response headers

Return value

\Symfony\Component\HttpFoundation\BinaryFileResponse The sitemap response object.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException If the sitemap is not found or the sitemap file is not readable.

1 call to XmlSitemapController::getSitemapResponse()
XmlSitemapController::renderSitemapXml in src/Controller/XmlSitemapController.php
Provides the sitemap in XML format.

File

src/Controller/XmlSitemapController.php, line 118

Class

XmlSitemapController
Class for Xml Sitemap Controller.

Namespace

Drupal\xmlsitemap\Controller

Code

public function getSitemapResponse($file, Request $request, array $headers = []) {
  if (!is_file($file) || !is_readable($file)) {
    $exception = new NotFoundHttpException();
    $exception
      ->setHeaders($headers);
    throw $exception;
  }
  $headers += [
    'Content-Type' => 'text/xml; charset=utf-8',
    'X-Robots-Tag' => 'noindex, follow',
  ];
  $lifetime = $this->configFactory
    ->get('xmlsitemap.settings')
    ->get('minimum_lifetime');
  $response = new BinaryFileResponse($file, 200, $headers);
  $response
    ->setPrivate();
  $response->headers
    ->addCacheControlDirective('must-revalidate');

  //if ($lifetime) {

  //  $response->headers->addCacheControlDirective('max-age', $lifetime);

  //}

  // Manually set the etag value instead of hashing the contents of the file.
  $last_modified = $response
    ->getFile()
    ->getMTime();
  $response
    ->setEtag(md5($last_modified));

  // Set expiration using the minimum lifetime.
  $response
    ->setExpires(new \DateTime('@' . ($last_modified + $lifetime)));

  // Because we do not want this page to be cached, we manually check the
  // modified headers.
  $response
    ->isNotModified($request);
  return $response;
}