vendor/kunstmaan/seo-bundle/Controller/RobotsController.php line 29

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\SeoBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Kunstmaan\SeoBundle\Entity\Robots;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. final class RobotsController extends AbstractController
  10. {
  11.     /** @var EntityManagerInterface */
  12.     private $em;
  13.     /** @var string */
  14.     private $robotsDefault;
  15.     public function __construct(EntityManagerInterface $emstring $robotsDefault)
  16.     {
  17.         $this->em $em;
  18.         $this->robotsDefault $robotsDefault;
  19.     }
  20.     /**
  21.      * Generates the robots.txt content when available in the database and falls back to normal robots.txt if exists
  22.      */
  23.     #[Route(path'/robots.txt'name'KunstmaanSeoBundle_robots'defaults: ['_format' => 'txt'])]
  24.     public function indexAction(Request $request): Response
  25.     {
  26.         $entity $this->em->getRepository(Robots::class)->findOneBy([]);
  27.         $robots $this->robotsDefault;
  28.         if ($entity && $entity->getRobotsTxt()) {
  29.             $robots $entity->getRobotsTxt();
  30.         } else {
  31.             $file $request->getBasePath() . 'robots.txt';
  32.             if (file_exists($file)) {
  33.                 $robots file_get_contents($file);
  34.             }
  35.         }
  36.         return $this->render('@KunstmaanSeo/Admin/Robots/index.html.twig', ['robots' => $robots]);
  37.     }
  38. }