I'm trying to pass query parameters received on a controller from an external redirect through TrustedRedirectResponse or LocalRedirectResponse. None of the options seem to be working.

有帮助吗?

解决方案

Receive the query parameters through the request you've added as parameter to the controller and pass them via the URL of the redirect.

Caching is a bit tricky because generating an absolute local URL bubbles up cacheable metadata which you need to collect by Url::toString(TRUE):

public function view(Request $request) {

  $url = Url::fromRoute(
    '<front>', 
    [], 
    [
      'query' => $request->query->all(),
      'absolute' => TRUE,
    ]
  )->toString(TRUE);

  $redirect = new LocalRedirectResponse($url->getGeneratedUrl());
  $redirect->addCacheableDependency($url);
  $redirect->getCacheableMetadata()->addCacheContexts(['url.query_args']);

  return $redirect;
}
许可以下: CC-BY-SA归因
scroll top