3 namespace Drupal\redirect_domain\EventSubscriber;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Path\PathMatcherInterface;
7 use Drupal\Core\Routing\TrustedRedirectResponse;
9 use Drupal\redirect\RedirectChecker;
10 use Symfony\Component\HttpKernel\KernelEvents;
11 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15 * Redirect subscriber for controller requests.
17 class DomainRedirectRequestSubscriber implements EventSubscriberInterface {
20 * @var \Drupal\redirect\RedirectChecker
22 protected $redirectChecker;
25 * Domain redirect configuration.
27 * @var \Drupal\Core\Config\Config
29 protected $domainConfig;
34 * @var \Drupal\Core\Path\PathMatcherInterface
36 protected $pathMatcher;
39 * Redirect configuration.
41 * @var \Drupal\Core\Config\Config
43 protected $redirectConfig;
46 * Constructs a \Drupal\redirect\EventSubscriber\RedirectRequestSubscriber object.
48 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
50 * @param \Drupal\redirect\RedirectChecker $redirect_checker
51 * The redirect checker service.
52 * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
55 public function __construct(ConfigFactoryInterface $config_factory, RedirectChecker $redirect_checker, PathMatcherInterface $path_matcher) {
56 $this->domainConfig = $config_factory->get('redirect_domain.domains');
57 $this->redirectConfig = $config_factory->get('redirect.settings');
58 $this->redirectChecker = $redirect_checker;
59 $this->pathMatcher = $path_matcher;
63 * Handles the domain redirect if any found.
65 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
66 * The event to process.
68 public function onKernelRequestCheckDomainRedirect(GetResponseEvent $event) {
69 $request = clone $event->getRequest();
71 if (!$this->redirectChecker->canRedirect($request)) {
75 // Redirect between domains configuration.
76 $domains = $this->domainConfig->get('domain_redirects');
77 if (!empty($domains)) {
78 $host = $request->getHost();
79 $path = $request->getPathInfo();
80 $protocol = $request->getScheme() . '://';
83 // Checks if there is a redirect domain in the configuration.
84 if (isset($domains[str_replace('.', ':', $host)])) {
85 foreach ($domains[str_replace('.', ':', $host)] as $item) {
86 if ($this->pathMatcher->matchPath($path, $item['sub_path'])) {
87 $destination = $item['destination'];
92 // Use the default status code from Redirect.
93 $response = new TrustedRedirectResponse(
94 $protocol . $destination,
95 $this->redirectConfig->get('default_status_code')
97 $event->setResponse($response);
105 * Prior to set the response it check if we can redirect.
107 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
109 * @param \Drupal\Core\Url $url
110 * The Url where we want to redirect.
112 protected function setResponse(GetResponseEvent $event, Url $url) {
113 $request = $event->getRequest();
115 parse_str($request->getQueryString(), $query);
116 $url->setOption('query', $query);
117 $url->setAbsolute(TRUE);
123 public static function getSubscribedEvents() {
124 // This needs to run before RouterListener::onKernelRequest(), which has
125 // a priority of 32 and
126 // RedirectRequestSubscriber::onKernelRequestCheckRedirect(), which has
127 // a priority of 33. Otherwise, that aborts the request if no matching
129 $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckDomainRedirect', 34];