my_module.my_entity.add:
route_name: node.add
title: 'Add My Entity'
route_parameters:
node_type: 'my_entity'
options:
query:
destination: '/path/to/redirect'
appears_on:
- my_module.my_entity.admin_content
\Drupal::destination()->get()
\Drupal::request()->query->remove('destination')
With a route name on a Controller:
use Drupal\Core\Controller\ControllerBase;
class MyControllerClass extends ControllerBase {
public function foo() {
//...
return $this->redirect('user.page');
}
}
With a absolute url:
return new RedirectResponse('https://google.com');
On a form RedirectResponse doesn't work, then:
With a route name:
public function submitForm(array &$form, FormStateInterface $form_state) {
//...
$form_state->setRedirect('user.page');
}
With a absolute url:
public function submitForm(array &$form, FormStateInterface $form_state) {
//...
$form_state->setResponse(new TrustedRedirectResponse('https://google.com', 302));
// You can change the response code from 302 to whatever you need
}