\Drupal::request() vs $request_stack->getCurrentRequest() These seem to return the same thing:

\Drupal::request()

$request_stack-&gt;getCurrentRequest()

Which is the preferred way to get the current request and why? Is one of these to be deprecated? Any other advantages/disadvantages?

Thanks!
How to get the current request object:

Procedural

In procedural code get the request from the static wrapper \Drupal:

$request = \Drupal::request();


Service

In a service get the request from the injected request stack:

$request = $this-&gt;requestStack-&gt;getCurrentRequest();


Controller

In a controller you can pull the request from the route argument stack by including a typed request parameter. An example from UserController:

use Symfony\Component\HttpFoundation\Request;

  public function resetPass(Request $request, $uid, $timestamp, $hash) {          
    // use $request
  }


It's not necessary to define the route parameter in the route definition. The request is always available.

Form

In a form method get the request from getRequest():

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $request = $this-&gt;getRequest();
    ...
  }


Don't use the injected requestStack property directly, because it is not available for all Form API callbacks.