0

In a custom module i'm using an EventSubscriber to redirect different people to different frontpages. Below is the simplified code I'm using.

//GET PATH
$nid = 1;
$domain_url = \Drupal::service('path_alias.manager')->getAliasByPath('/node/'. $nid);

//FRONTPAGE REDIRECT
$response = new RedirectResponse($domain_url);
$event->setResponse($response);

The module is working but there is one detail I can't seem to solve.

For example on my website domain.com I have node/1 that has an alias path /home.

When going to domain.com it is, because of my module as expected, redirected to domain.com/home. But what I want is the url not to show the home part. Is it possible to show the content of /home but keeping the url domain.com?

4
  • 1
    If you don't want to change the URL in the browser then don't redirect. From the event subscriber you could start a sub request. Or change the incoming path in path processing. For the latter @Jaypan has developed a solution by making path processing domain aware. See drupal.stackexchange.com/questions/288321/…
    – 4uk4
    Nov 15, 2020 at 16:15
  • What @Jaypan has done...is exactly what I need but I can't figure out (by the post) what he did. It doesn't look like the full code. I'll try and figure out how path processing is working..
    – Joost
    Nov 18, 2020 at 7:26
  • For I've overriden Drupal\Core\Routing\RouteProvider::getRouteCollectionCacheId() see bounteous.com/insights/2017/04/19/…
    – 4uk4
    Nov 18, 2020 at 9:01
  • Thanks! I ended up using large parts of the PathProcessorFront.php from core to get my module to work. Only thing is that I have a caching problem I'll create a new post for this.
    – Joost
    Nov 18, 2020 at 15:58

1 Answer 1

0

You can add the path to your homepage from admin/config/system/site-information

And replace /node/1 to /home.

You don't need an even subscriber to do that.

2
  • I know the default way of setting the frontpage. I'm working with a custom module to be able to set multiple frontpages.
    – Joost
    Nov 15, 2020 at 8:17
  • I would rather have one frontpage which would serve the content dynamically even if it needs multiple rules to load it and keep the default way of setting the frontpage.
    – MacSim
    Nov 15, 2020 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.