drupal 8: how to redirect route with route parameters?

Viewed 10809

I have created dynamic route in drupal8 with parameter. Now i need to redirect to this page based on user action. How can this be achieved?

Page sample router code as below example:

domain_site_settings.config_form:
  path: '/admin/config/domain/domain_site_settings/{domain_id}/edit'
  defaults:
    _form: '\Drupal\domain_site_settings\Form\DomainConfigSettingsForm'
    _title: 'Domain site settings'
  requirements:
    _permission: 'domain site settings'
  options:
    _admin_route: TRUE
3 Answers

Finally i found answer for this question.

use Symfony\Component\HttpFoundation\RedirectResponse;

$path = \Drupal\Core\Url::fromRoute('domain_site_settings.config_form',
    ['domain_id' => 'drupal_com'])->toString();
  $response = new RedirectResponse($path);
  $response->send();
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;  


$url = Url::fromRoute('YOUR CUSTOM ROUTE');
  $query = [
    'searchQuery' => [
      'searchType' => $searchType,
      'title'      => $title,
    ],
  ];
  $path = $url->setOption('query', $query);
  $path = $path->toString();

  $response = new RedirectResponse($path);
  $response->send();
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;

...

$url = Url::fromRoute('example_route.subpath', [
  'param1' => 23,
  'param2' => 'xyz'
]);
return new RedirectResponse($url->toString());

Or if you have access to FormStateInterface object

$form_state->setRedirect('example_route.subpath', [
  'param1' => 23,
  'param2' => 'xyz'
]);
Related