Example 1. Redirect to the front page:
return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url('<front>'));
Example 2. Redirect to a route path (user page):
return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url('user.page'));
Example 3. To a internal path
return new \Symfony\Component\HttpFoundation\RedirectResponse('/node/17/edit');
OR
return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal\Core\Url::fromUserInput('/node/17/edit')->toString());
Example 4. Redirect to Access Denied (403) or Not Found (404) page.
403:
throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException();
404:
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url('<front>'));
Example 2. Redirect to a route path (user page):
return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url('user.page'));
Example 3. To a internal path
return new \Symfony\Component\HttpFoundation\RedirectResponse('/node/17/edit');
OR
return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal\Core\Url::fromUserInput('/node/17/edit')->toString());
Example 4. Redirect to Access Denied (403) or Not Found (404) page.
403:
throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException();
404:
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
Comments
Post a Comment