Why is requesting an object in the Symfony 2.8 service empty?

Symfony 2.8 has this service:

services:
    app.admin_menu:
        class: GRF\AdminPanelBundle\Service\Menu
        shared: false
        arguments: [@request_stack]

and in the service design:

public function __construct(RequestStack $request)
{
    exit(var_dump($request));
}

and in the browser:

object(Symfony\Component\HttpFoundation\RequestStack)#261 (1) { ["requests":"Symfony\Component\HttpFoundation\RequestStack":private]=> array(0) { } }

how to access the current request in the service ??? now the current request is null

+4
source share
2 answers

I had a similar problem .

The solution is to try to access the RequestStack object as long as possible to give the kernel the opportunity to populate it.

Essentially, do not try to access it in the constructor.

I moved his access to the method, which is called later, and he solved it for me.

private $requestStack;

public function __construct(RequestStack $request)
{
    $this->requestStack = $request;
}

public function outputRequestStack() {
    exit(var_dump($this->requestStack));
}
+2
source

RequestStack - , ( esi). Request, $request = requestStack->getMasterRequest();

+1

Source: https://habr.com/ru/post/1648684/


All Articles