SimplesitemapController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Drupal\simple_sitemap\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Drupal\simple_sitemap\Simplesitemap;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Drupal\simple_sitemap\SimplesitemapManager;
  10. /**
  11. * Class SimplesitemapController
  12. * @package Drupal\simple_sitemap\Controller
  13. */
  14. class SimplesitemapController extends ControllerBase {
  15. /**
  16. * @var \Drupal\simple_sitemap\Simplesitemap
  17. */
  18. protected $generator;
  19. /**
  20. * SimplesitemapController constructor.
  21. * @param \Drupal\simple_sitemap\Simplesitemap $generator
  22. */
  23. public function __construct(Simplesitemap $generator) {
  24. $this->generator = $generator;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function create(ContainerInterface $container) {
  30. return new static(
  31. $container->get('simple_sitemap.generator')
  32. );
  33. }
  34. /**
  35. * Returns the whole sitemap variant, its requested chunk,
  36. * or its sitemap index file.
  37. * Caches the response in case of expected output, prevents caching otherwise.
  38. *
  39. * @param string $variant
  40. * Optional name of sitemap variant.
  41. * @see \hook_simple_sitemap_variants_alter()
  42. * @see SimplesitemapManager::getSitemapVariants()
  43. *
  44. * @param \Symfony\Component\HttpFoundation\Request $request
  45. * The request object.
  46. *
  47. * @throws NotFoundHttpException
  48. *
  49. * @return object
  50. * Returns an XML response.
  51. */
  52. public function getSitemap(Request $request, $variant = NULL) {
  53. $output = $this->generator->setVariants($variant)->getSitemap($request->query->getInt('page'));
  54. if (!$output) {
  55. throw new NotFoundHttpException();
  56. }
  57. return new Response($output, Response::HTTP_OK, [
  58. 'content-type' => 'application/xml',
  59. 'X-Robots-Tag' => 'noindex', // Tell search engines not to index the sitemap itself.
  60. ]);
  61. }
  62. }