creating a function that can create GET url variables as object when I need it
If these $_GET parameters are also route parameters (set up in a module's routing configuration), you can use

 \Drupal::routeMatch()-&gt;getParameters();


Or for a specific parameter:

$keyword = \Drupal::routeMatch()-&gt;getParameters()-&gt;get('keyword');


If the $_GET parameters are not route parameters, you can get all the parameters this way:

$params = \Drupal::request()-&gt;query-&gt;all();


If you want to set default parameters for parameters that aren't in the URL, you can merge the $params with an array of defaults.  Then depending on how you're going to use the parameters, you may want to XSS filter the params to prevent cross-site scripting attempts.  As @Berdir points out, this is rarely necessary, as twig auto escapes and if you are passing parameters into the query abstraction layer properly, it will also filter your input.  But you could filter your params this way:

foreach ($params as $key =&gt; $value) {
  if (!empty($value)) {
    $params[$key] = Xss::filter($value);
  }
}


(If you are using arrays in your $_GET params, this foreach loop would need to be altered to handle that use case.)

Here's some more info on XSS security.

After you have pulled-in and cleaned up your $_GET variables, you could then save these parameters to your object, and then use them conditionally in your query.

Since you asked specifically about Controllers, you can pass the request object to controller in the constructor:

  public function __construct(Request $request) {
    $this-&gt;request = $request;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container-&gt;get('request_stack')-&gt;getCurrentRequest()
    );
  }


Then you could use the request object from the container.

If you set up the parameters as route parameters, you can just pass them in as arguments to the controller.