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