Hi,

I have an array that looks like this:

sourceid = [[0] => 10586, [1] => 10589, [2] => 10605]

I want to attach this array as url parameters such that my url looks like:

/page/text?sourceid = 10586 & sourceid = 10589 & sourceid = 10605

Later I want to recover these sourceid using $_GET['sourceid] in a different function.

As long as I was attaching a single parameter, I could use the GET function to get the value of the sourceid from url. Now with an array of values, I dont know how to do it.

So far I have tried:

$query = http_build_query(['sourceid' => $sourceid]);
$url = Url::fromRoute('heritage_ui.addpage', ['textid' => $textid]);

$url->setOption('query', [
$query,
]);

But it does not seem to do the job.

Thanks for your help.

Comments

wombatbuddy’s picture

I don't know the way to create a query string which contains pairs with the same keys.
But I think that you can pass your original array.
See the example: 

$url = Url::fromRoute('my_module.default_controller');

$query = [
  'sourceid' => [
    '1',
    '2',
    '3',
  ],
];

$url->setOption('query', $query);

Then you can get array with source ids with the code like this: 

$sourceid = \Drupal::request()->query->get('sourceid');

or in this way: 

$sourceid = $_GET['sourceid']);
Saptaparnee’s picture

Ok, thanks.

wdev’s picture

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