18

These seem to return the same thing:

\Drupal::request()

$request_stack->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!

2
  • 3
    What context are you calling it in? Generally speaking if your context supports dependency injection, you would inject the request stack service and use it the latter way (better for testability). If you're in procedural code, e.g. a hook, you would use the former Commented Oct 10, 2018 at 22:15
  • Thanks, @Clive ... it is in a service that I didn't originally write and I had seen the other way from more time in hooks, but that makes sense. Thanks for the clarification! Commented Oct 11, 2018 at 14:19

1 Answer 1

25

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 current request from the injected argument @request_stack:

The module_name/module_name.services.yml file :

services: custom.service: class: Drupal\module_name\Service\CustomService arguments: - '@request_stack' 

The module_name/src/Service/CustomService.php file :

use Symfony\Component\HttpFoundation\RequestStack; /** @var \Symfony\Component\HttpFoundation\RequestStack */ protected $requestStack; class customService { public function __construct(RequestStack $requestStack) { $this->requestStack = $requestStack; } public function doSomething() { // use $this->requestStack->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 &$form, FormStateInterface $form_state) { $request = $this->getRequest(); ... } 

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

5
  • Thanks! ... it is in a service that I didn't originally write and I had seen the other way from more time spent in hooks, but that makes sense. Thanks for the clarification! Commented Oct 11, 2018 at 14:20
  • How does a service get the injected request stack? Commented May 8, 2019 at 15:06
  • 4
    @joachim, the service needs @request_stack as argument. You find examples in core.services.yml. Commented May 8, 2019 at 16:33
  • @4k4 Could you give us an example scenario of when the requestStack wouldn't be available if injected directly? Commented Aug 26, 2019 at 21:04
  • @HeitorAlthmann, if injected directly it would be available, but you can't be sure it is when using the form class in Form API. $request = $this->getRequest() gets the request from the injected service, but has a fallback in case it is not available. Commented Aug 27, 2019 at 6:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.