Score:0

How to get route inside RequestEvent

ru flag

I need to redirect all existing links targeting a few hand-picked nodes to an external domain. I created an EventSubscriber listening to RequestEvents

my.services.yml

services:
  my.request_subscriber:
    class: Drupal\my\EventSubscriber\EventSubscriber
    arguments: ['@current_route_match', '@redirect.checker']
    tags:
      - { name: event_subscriber }

EventSubscriber.php

/* trimmed constructor, etc. for simplicity */

  public static function getSubscribedEvents() {
    // This needs to run before Redirect module
    $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect', 34];
    return $events;
  }
  public function onKernelRequestCheckRedirect(RequestEvent $event) {
    // the follwing lines are taken from redirect module 
    // @see https://git.drupalcode.org/project/redirect/-/blob/8.x-1.x/src/EventSubscriber/RedirectRequestSubscriber.php#L111
    $request = clone $event->getRequest();
    if (!$this->checker->canRedirect($request)) {
      return;
    }

    /**
     * No matter which route I visit, the $this->routeMatch->getRouteObject is always null,
     * therefore the following lines also don't work as I hoped
     */
    $routeName = $this->routeMatch->getRouteName(); // returns NULL :(
    $nodeId = $this->routeMatch->getParameter('node'); // returns NULL :(

    if ($routeName !== 'entity.node.canonical' || !in_array($nodeId, [MY_HANDPICKED_NODE_IDS]) ) {
      return;
    }
    else {
       // do $event->setResponse(my TrustedRedirectResponse);
    }
}

Why is my route object always NULL? How do I get the routing information in my subscriber?

No Sssweat avatar
ua flag
For debugging/testing purposes, if you try `\Drupal::routeMatch()->getRouteName();` is it also NULL ?
ru flag
Yes, same as injected, all route information is NULL
4uk4 avatar
cn flag
Why did you choose the priority 34? Routing is at priority 32. The default priority 0 should work most times unless you need to run before a specific core subscriber.
ru flag
@4uk4 Thanks, this was the issue. I was taking a look at the code in the redirect module, they used `34` there and I thought this is good for me too. Simply removing/using the default priority fixed it `$events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect'];`
Score:2
ru flag

The problem was caused by the priority inside the subscribe function. The redirect module used priority 34 here, but this was not fitting my case. Simply removing/using the default priority in the subscribe function solved the issue.

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect'];
    return $events;
  }

This answer is coming from @4uk4's comment below the original question.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.