creating a function that can create GET url variables as object when I need it I am trying to parse my form's GET method and create a function that can get all the parameters from the URL in DRUPAL 8. I have a form that I am gonna use it to filter my library items as search( I know I can use search api or advanced search but my client wants this way ) So I have a few fields such as keyword, title, description, location, author, from, to, collection. and when I add all the fields after the redirection to result page

http://example.com/advancedsearchresult?keyword=test1&amp;title=test2&amp;description=test3&amp;author=test4&amp;location=test5&amp;from=&amp;to=&amp;collection=&amp;media=all


I would like to do nice job with using OOP and Symfony service features. My question is how could i create a function in my controller for this result page that is gonna parse all the parameters from the URL so I can use them to create an object in service to create individual query for each parameters to make search mechanism for this form?

UPDATED:

After @oknate pointed out, I updated my controller like this:

namespace Drupal\advancedsearch\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\advancedsearch\AdvancedSearchQuery;

class SearchResultController extends ControllerBase {
 protected $advancedsearchSearchResult;

public function __construct(AdvancedSearchQuery $advancedsearch_search_result ) {
 $this-&gt;advancedsearchSearchResult = $advancedsearch_search_result;
}

public static function create(ContainerInterface $container) {
return new static(
  $container-&gt;get('advancedsearch.search.result')
);
}
private function setupAdvancedSearchQuery() {
$params = \Drupal::request()-&gt;query-&gt;all();
foreach ($params as $key =&gt; $value) {
  if (!empty($value)) {
    $params[$key] = $value;
  }
}
}
}


on the other hand, I created my service file, I created my variables which matches with parameters, so am I gonna be able to use setupAdvancedSearchQuery() when I call the object for each parameters, if so can anyone give a hint?
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.