Non-existent service “request_stack”

后端 未结 2 1379
失恋的感觉
失恋的感觉 2021-01-18 05:09

I am writing a Symfony 2.6 application and I\'ve encountered a problem when trying to inject the RequestStack into a service. What I want is to be able to get the current re

2条回答
  •  有刺的猬
    2021-01-18 05:55

    Since 2017 and Symfony 3.3+ this is now very simple.

    1. Register services with autowiring via PSR-4 service autodiscovery

    # app/config/services.yml
    services:
        _defaults:
            autowire: true
    
        App\:
           resource: ../../src/App
    

    2. Require RequestStack in service via Constructor Injection

    requestStack = $requestStack;
        }
    
        public function someMethod()
        {
            $request = $this->requestStack->getCurrentRequest();
            // ...
        }
    }
    

    3. Or in Controllers, require Request as action method argument

    ...;
        }
    }
    

    Nothing more is needed.

    Happy coding!

提交回复
热议问题