【问题标题】:drupal 8: how to redirect route with route parameters?drupal 8:如何使用路由参数重定向路由?
【发布时间】:2019-02-22 18:06:54
【问题描述】:

我在 drupal8 中创建了带参数的动态路由。现在我需要根据用户操作重定向到此页面。如何实现?

页面示例路由器代码如下示例:

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

【问题讨论】:

  • 其他用户将您的问题标记为低质量和需要改进。我重新措辞/格式化您的输入,使其更容易阅读/理解。请查看我的更改以确保它们反映您的意图。但我认为你的问题仍然无法回答。 现在应该edit你的问题,包括你自己的努力(见help me is not a question)。如果您对我有其他问题或反馈,请随时给我留言。

标签: drupal-8


【解决方案1】:

我终于找到了这个问题的答案。

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();

【讨论】:

  • 我只想提一下,如果这是在路由控制器中,Symfony 期望控制器返回一个 Response 对象。无需调用 $response->send(),只需返回重定向响应即可。
【解决方案2】:
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();

【讨论】:

    【解决方案3】:
    use Drupal\Core\Url;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    
    ...
    
    $url = Url::fromRoute('example_route.subpath', [
      'param1' => 23,
      'param2' => 'xyz'
    ]);
    return new RedirectResponse($url->toString());
    

    或者如果您有权访问FormStateInterface 对象

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

    【讨论】:

      猜你喜欢
      • 2019-11-12
      • 2015-08-08
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      相关资源
      最近更新 更多