Skip to main content

How to do a RedirectResponse with a destination query parameter

How to do a RedirectResponse with a destination query parameter

 

Drupal 8 and on, is well integrated with Symfony technology and object 

good example is the Response with a redirect

use Symfony\Component\HttpFoundation\RedirectResponse;

$target = 65; // a parameter you want to add
$url = Url::fromUri('internal:/node/add/page'); // choose a path
$link_options = array(
  'attributes' => array(
     'class' => array(
        'btn',
     ),
   ),
   'query' => array(
     'target' => $target
   )
);
$url->setOptions($link_options);
$destination = $url->toString();

$response = new RedirectResponse($destination);
$response->send();

 

$redirectURL = '/cart/add/p2';
return new RedirectResponse(base_path().$redirectURL);

you can also use Drupal HttpFoundation library

object: SecuredRedirectResponse

Provides a standard base class for safe redirects.

In case you want to redirect to external URLs use TrustedRedirectResponse.

For local URLs, we use LocalRedirectResponse which opts out of external redirects.