vendor/contao/core-bundle/src/Controller/RobotsTxtController.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Controller;
  11. use Contao\CoreBundle\Event\ContaoCoreEvents;
  12. use Contao\CoreBundle\Event\RobotsTxtEvent;
  13. use Contao\CoreBundle\Framework\ContaoFramework;
  14. use Contao\PageModel;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use webignition\RobotsTxt\File\Parser;
  20. /**
  21.  * @Route(defaults={"_scope" = "frontend"})
  22.  *
  23.  * @internal
  24.  */
  25. class RobotsTxtController
  26. {
  27.     /**
  28.      * @var ContaoFramework
  29.      */
  30.     private $contaoFramework;
  31.     /**
  32.      * @var EventDispatcherInterface
  33.      */
  34.     private $eventDispatcher;
  35.     public function __construct(ContaoFramework $contaoFrameworkEventDispatcherInterface $eventDispatcher)
  36.     {
  37.         $this->contaoFramework $contaoFramework;
  38.         $this->eventDispatcher $eventDispatcher;
  39.     }
  40.     /**
  41.      * @Route("/robots.txt")
  42.      */
  43.     public function __invoke(Request $request): Response
  44.     {
  45.         $this->contaoFramework->initialize();
  46.         /** @var PageModel $pageModel */
  47.         $pageModel $this->contaoFramework->getAdapter(PageModel::class);
  48.         /** @var PageModel|null $rootPage */
  49.         $rootPage $pageModel->findPublishedFallbackByHostname(
  50.             $request->server->get('HTTP_HOST'),
  51.             ['fallbackToEmpty' => true]
  52.         );
  53.         if (null === $rootPage) {
  54.             return new Response(''Response::HTTP_NOT_FOUND);
  55.         }
  56.         $parser = new Parser();
  57.         $parser->setSource((string) $rootPage->robotsTxt);
  58.         $file $parser->getFile();
  59.         $this->eventDispatcher->dispatch(new RobotsTxtEvent($file$request$rootPage), ContaoCoreEvents::ROBOTS_TXT);
  60.         return new Response((string) $file200, ['Content-Type' => 'text/plain; charset=UTF-8']);
  61.     }
  62. }