vendor/contao/core-bundle/src/Routing/FrontendLoader.php line 37

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\Routing;
  11. use Contao\CoreBundle\ContaoCoreBundle;
  12. use Symfony\Component\Config\Loader\Loader;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. class FrontendLoader extends Loader
  16. {
  17.     /**
  18.      * @var bool
  19.      */
  20.     private $prependLocale;
  21.     /**
  22.      * @var string
  23.      */
  24.     private $urlSuffix;
  25.     /**
  26.      * @internal Do not inherit from this class; decorate the "contao.routing.frontend_loader" service instead
  27.      */
  28.     public function __construct(bool $prependLocalestring $urlSuffix '.html')
  29.     {
  30.         trigger_deprecation('contao/core-bundle''4.10''Using the "Contao\CoreBundle\Routing\FrontendLoader" class has been deprecated and will no longer work in Contao 5.0. Use Symfony routing instead.'E_USER_DEPRECATED);
  31.         $this->prependLocale $prependLocale;
  32.         $this->urlSuffix $urlSuffix;
  33.     }
  34.     public function load($resource$type null): RouteCollection
  35.     {
  36.         $routes = new RouteCollection();
  37.         $defaults = [
  38.             '_token_check' => true,
  39.             '_controller' => 'Contao\CoreBundle\Controller\FrontendController::indexAction',
  40.             '_scope' => ContaoCoreBundle::SCOPE_FRONTEND,
  41.         ];
  42.         $this->addFrontendRoute($routes$defaults);
  43.         $this->addIndexRoute($routes$defaults);
  44.         return $routes;
  45.     }
  46.     public function supports($resource$type null): bool
  47.     {
  48.         return 'contao_frontend' === $type;
  49.     }
  50.     /**
  51.      * Adds the frontend route, which is language-aware.
  52.      */
  53.     private function addFrontendRoute(RouteCollection $routes, array $defaults): void
  54.     {
  55.         $route = new Route('/{alias}'.$this->urlSuffix$defaults, ['alias' => '.+']);
  56.         $this->addLocaleToRoute($route);
  57.         $routes->add('contao_frontend'$route);
  58.     }
  59.     /**
  60.      * Adds a route to redirect a user to the index page.
  61.      */
  62.     private function addIndexRoute(RouteCollection $routes, array $defaults): void
  63.     {
  64.         $route = new Route('/'$defaults);
  65.         $this->addLocaleToRoute($route);
  66.         $routes->add('contao_index'$route);
  67.     }
  68.     /**
  69.      * Adds the locale to the route if prepend_locale is enabled.
  70.      */
  71.     private function addLocaleToRoute(Route $route): void
  72.     {
  73.         if (!$this->prependLocale) {
  74.             return;
  75.         }
  76.         $route->setPath('/{_locale}'.$route->getPath());
  77.         $route->addRequirements(['_locale' => '[a-z]{2}(\-[A-Z]{2})?']);
  78.     }
  79. }