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
  21. */
  22. public function __construct(bool $prependLocale, string $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($resource, ?string $type = null): RouteCollection
  29. {
  30. $routes = new RouteCollection();
  31. $defaults = [
  32. '_controller' => 'Contao\CoreBundle\Controller\FrontendController::indexAction',
  33. '_scope' => ContaoCoreBundle::SCOPE_FRONTEND,
  34. ];
  35. $this->addFrontendRoute($routes, $defaults);
  36. $this->addIndexRoute($routes, $defaults);
  37. return $routes;
  38. }
  39. public function supports($resource, ?string $type = null): bool
  40. {
  41. return 'contao_frontend' === $type;
  42. }
  43. /**
  44. * Adds the frontend route, which is language-aware.
  45. */
  46. private function addFrontendRoute(RouteCollection $routes, array $defaults): void
  47. {
  48. $route = new Route('/{alias}'.$this->urlSuffix, $defaults, ['alias' => '.+']);
  49. $this->addLocaleToRoute($route);
  50. $routes->add('contao_frontend', $route);
  51. }
  52. /**
  53. * Adds a route to redirect a user to the index page.
  54. */
  55. private function addIndexRoute(RouteCollection $routes, array $defaults): void
  56. {
  57. $route = new Route('/', $defaults);
  58. $this->addLocaleToRoute($route);
  59. $routes->add('contao_index', $route);
  60. }
  61. /**
  62. * Adds the locale to the route if prepend_locale is enabled.
  63. */
  64. private function addLocaleToRoute(Route $route): void
  65. {
  66. if (!$this->prependLocale) {
  67. return;
  68. }
  69. $route->setPath('/{_locale}'.$route->getPath());
  70. $route->addRequirements(['_locale' => '[a-z]{2}(\-[A-Z]{2})?']);
  71. }
  72. }