2

Right now I'm doing this ugly thing to get the url of the my set frontpage (skipping validations and making sure it exists since I'm not sure if I'll even use this approach)

$url = $this->config('system.site')->get('page')['front'];
return new RedirectResponse($url);

This works but seems ugly, bloated and wrong.

Update:

As pointed out in the comments, this may be a matter of personal taste, therefore the question would be deemed subjective and it can be closed. It surely isn't a duplicate, I'm not asking how to redirect in general, it's a specific redirection, perhaps I could've worded it differently.

  • What's ugly about it? What would a beautiful version look like exactly? Where else do you want to get the data from if not config? How would you store it if not keyed by string? Without knowing what you consider to be the correct approach, this basically can't be answered. Alternatives may be listed, but how could we know if they meet your standards either? – Clive Mar 15 '17 at 13:23
  • If you don't see anything wrong with it, I guess it's a matter of personal taste then. I wasn't sure if accessing the configuration as such was reliable. To whomever downvoted my question, care to explain why? – Juan Mar 15 '17 at 13:26
  • not a duplicate, linked question is for a redirect to any url in a hook, this couldn't be more different, it is for a controller and a redirect to the frontpage, for which there is a route predefined in D8 – 4k4 Mar 15 '17 at 14:01
  • I was deliberately trying to make that comment un-opinionated @JuanCortés - "ugly", "bloated" and "wrong" are all rather negatively charged words, which isn't really a problem, but without knowing what you would consider to be a positive DX around the same thing, it's difficult to respond with anything other than an opinion. If it's based on something tangible (e.g it's a lot of typing, there's unchecked function return array dereferencing going on, etc), that would give answerers something more to get their teeth into – Clive Mar 15 '17 at 14:07
  • You're right, I approached this question putting my own answer down from the get go, thanks for taking the time to discuss it. @Clive It's absurd that a question can be marked as an exact duplicate of another one by 5 different mods, that have clearly not read my question. Working as automatons and only reading the titles ensure false positives, this is one of those cases. – Juan Mar 16 '17 at 10:56
6

Use the route <front> and when you are in a controller use the injected method redirect():

return $this->redirect('<front>');
  • This is what I was after, I was sure I had seen something like it before. It even works from a form submission with $form_state->setRedirect('<front>'). Cheers – Juan Mar 16 '17 at 10:57
  • In case someone finds some weird behavior when for example setting cookies before the redirect, you should actually use return $this->redirect('<front>')->send(); Hope it saves someone the time I spent hitting my head against the table for this. – Marc Feb 1 at 17:20
  • @Marc, don't send a response in Drupal. Even if it works now in dev this will cause unpredictable behavior in production. – 4k4 Feb 1 at 17:40

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.