3 namespace Drupal\simple_sitemap\Controller;
5 use Drupal\Core\Cache\CacheableResponse;
6 use Drupal\Core\Controller\ControllerBase;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10 use Drupal\simple_sitemap\Simplesitemap;
13 * Class SimplesitemapController
14 * @package Drupal\simple_sitemap\Controller
16 class SimplesitemapController extends ControllerBase {
19 * The sitemap generator.
21 * @var \Drupal\simple_sitemap\Simplesitemap
26 * SimplesitemapController constructor.
28 * @param \Drupal\simple_sitemap\Simplesitemap $generator
29 * The sitemap generator.
31 public function __construct(Simplesitemap $generator) {
32 $this->generator = $generator;
36 * Returns the whole sitemap, a requested sitemap chunk, or the sitemap index file.
38 * @param int $chunk_id
39 * Optional ID of the sitemap chunk. If none provided, the first chunk or
40 * the sitemap index is fetched.
42 * @throws NotFoundHttpException
45 * Returns an XML response.
47 public function getSitemap($chunk_id = NULL) {
48 $output = $this->generator->getSitemap($chunk_id);
50 throw new NotFoundHttpException();
53 // Display sitemap with correct XML header.
54 $response = new CacheableResponse($output, Response::HTTP_OK, ['content-type' => 'application/xml']);
55 $meta_data = $response->getCacheableMetadata();
56 $meta_data->addCacheTags(['simple_sitemap']);
63 public static function create(ContainerInterface $container) {
64 return new static($container->get('simple_sitemap.generator'));