Steps for generating links with query parameters.
1. Create an Url with query parameters using the core Url class.
// Set query parameter in option array.
$options = [
'query' => [
'xyz_param' => 'param_value',
],
];
// Create url.
$url = Url::fromRoute('entity.node.canonical', ['node' => 1], $options);
// We can also use setOptions to add/update query parameters.
$url->setOptions($options);
/*
* Other url functions
* - \Drupal\Core\Url::fromRoute($route_name, $route_parameters = [], $options = [])
* - \Drupal\Core\Url::fromRouteMatch(RouteMatchInterface $route_match)
* - \Drupal\Core\Url::fromUri($uri, $options = [])
* - \Drupal\Core\Url::fromUserInput($user_input, $options = [])
*/
2. Create Link from URL
// Create Link
$link = Link::fromTextAndUrl('Link Text', $url);
// Link to string - if needed.
$link = Link::fromTextAndUrl('Link Text', $url)->toString();
With Twig
{% set url = url('entity.node.canonical',{'node':'1'},{'query':{'xyz_param':'param_value'}})|render %}
{{ link("Link Text", url, { 'class':['btn', 'btn-primar']} ) }}
Retrieve path query parameters in \Drupal::request()
\Drupal::request()->get('xyz_param');
// OR
\Drupal::request()->query->get('xyz_param');