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

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.     private ContaoFramework $framework;
  28.     private EventDispatcherInterface $eventDispatcher;
  29.     public function __construct(ContaoFramework $frameworkEventDispatcherInterface $eventDispatcher)
  30.     {
  31.         $this->framework $framework;
  32.         $this->eventDispatcher $eventDispatcher;
  33.     }
  34.     /**
  35.      * @Route("/robots.txt")
  36.      */
  37.     public function __invoke(Request $request): Response
  38.     {
  39.         $this->framework->initialize();
  40.         $pageModel $this->framework->getAdapter(PageModel::class);
  41.         $rootPage $pageModel->findPublishedFallbackByHostname(
  42.             $request->getHost(),
  43.             ['fallbackToEmpty' => true]
  44.         );
  45.         if (null === $rootPage) {
  46.             return new Response(''Response::HTTP_NOT_FOUND);
  47.         }
  48.         $parser = new Parser();
  49.         $parser->setSource((string) $rootPage->robotsTxt);
  50.         $file $parser->getFile();
  51.         $this->eventDispatcher->dispatch(new RobotsTxtEvent($file$request$rootPage), ContaoCoreEvents::ROBOTS_TXT);
  52.         return new Response((string) $file200, ['Content-Type' => 'text/plain; charset=UTF-8']);
  53.     }
  54. }