Drupal 8 redirect example

Sometimes it's necessary when you build custom drupal module. You can use RedirectResponse():
  1. use Symfony\Component\HttpFoundation\RedirectResponse;
  2. use Drupal\Core\Url;
  3.  
  4. $response = new RedirectResponse('/node/'.$entity->id().'/edit');
  5. $response->send();
  6.  
  7. //or
  8.  
  9. $url = Url::fromRoute('mypage.view');
  10. $response = new RedirectResponse($url);
  11. $response->send();