I am attempting to add some code to a hook_entity_view_alter()
function to redirect the user to the homepage under a rare condition with RedirectResponse
.
I understand the method to accomplish this is along these lines (taken from here):
$response = new Symfony\Component\HttpFoundation\RedirectResponse($url);
$response->send(); // don't send the response yourself inside controller and form.
return;
My issue is that I am not sure how to format $url
. Most articles on the topic, such as this one, suggest using \Drupal::url()
, but this is deprecated in favour of Drupal\Core\Url
. I would like to avoid using deprecated methods as much as possible, but am stuck at trying to figure out what the Drupal\Core\Url
equivalent is to \Drupal::url('<front>')
. I am not sure which methods to be using, and nothing I've tried with Url::fromUri()
has worked.