src/Controller/RobotsTxtController.php line 32

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-18
  5.  * Time: 21:17
  6.  */
  7. namespace App\Controller;
  8. use App\Http\Crawler\CrawlerDetector;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class RobotsTxtController extends AbstractController
  16. {
  17.     protected CrawlerDetector $crawlerDetector;
  18.     public function __construct(CrawlerDetector $crawlerDetector null)
  19.     {
  20.         $this->crawlerDetector $crawlerDetector ?? new CrawlerDetector();
  21.     }
  22.     /**
  23.      * @see https://redminez.net/projects/belochki/wiki/%D0%A0%D0%B5%D0%B4%D0%B8%D1%80%D0%B5%D0%BA%D1%82%D1%8B_%D0%B4%D0%BB%D1%8F_%D0%9F%D0%A1_%D0%B8_%D0%A0%D0%9A%D0%9D
  24.      */
  25.     #[Route(path'/robots.txt')]
  26.     #[Cache(smaxage0)]
  27.     public function page(Request $requestParameterBagInterface $parameterBag): Response
  28.     {
  29.         $currentHost $request->getHost();
  30.         $mainDomain $parameterBag->get('app.main_domain');
  31.         if ($currentHost !== $mainDomain && !preg_match('/\.'.preg_quote($mainDomain).'$/'$currentHost)) {
  32.             // Вход на один из старых доменов
  33.             $userAgent $request->headers->get('User-Agent''');
  34.             if (!$this->crawlerDetector->isCrawler($userAgent)) {
  35.                 // Для не-ботов ПС в качестве хоста отдаем корень старого домена
  36.                 $hostParts explode('.'$currentHost);
  37.                 $count count($hostParts);
  38.                 $mainDomain sprintf('%s.%s'$hostParts[$count-2], $hostParts[$count-1]);
  39.             }
  40.         }
  41.         $sitemapHttps $parameterBag->get('app.sitemap.https');
  42.         $sitemapGzip $parameterBag->get('app.sitemap.gzip');
  43.         $sitemapDirectory $parameterBag->get('app.sitemap.directory');
  44.         $sitemapPath rtrim($sitemapDirectory'/').'/sitemap.xml';
  45.         if (true === $sitemapGzip) {
  46.             $sitemapPath .= '.gz';
  47.         }
  48.         $sitemapUrl sprintf('%s://%s/%s'true === $sitemapHttps 'https' 'http'$mainDomainltrim($sitemapPath'/'));
  49.         $schemePrefix $request->isSecure() ? 'https_' 'http_';
  50.         $view $this->renderView("{$schemePrefix}robots.txt.twig", [
  51.             'host' => $mainDomain,
  52.             'sitemap_url' => $sitemapUrl,
  53.         ]);
  54.         $response = new Response($viewResponse::HTTP_OK, [
  55.             'Content-Type' => 'text/plain; charset=utf-8',
  56.         ]);
  57.         return $response;
  58.     }
  59. }