O
O
Outsider V.2017-07-15 11:43:13
symfony
Outsider V., 2017-07-15 11:43:13

How to access Request inside normal controller method?

Symfony 3.4. Inside the controller method, you need to get the value of the placeholder, for this you need the current Request. But the developers for some reason did not leave the possibility to get a Request via $this->getRequest() or $this->container->get('request'), access works only through injection and only in actions:

* @Route("/{id}", name="delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request)
    {

How can I get to it now directly from the usual method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2017-07-15
@Audiophile

$this->get('request_stack')->getCurrentRequest()
The Request itself is the "input" options object for your application via the http abstraction. A similar object is the console object Input.
Controllers are essentially a wrapper for http and that is why the Symphony Request is injected there.
In the top three, the "request" service was cut to hell, for which thanks to the guys. Instead, now RequestStack, which is always there, but may not contain the Request object (saves a lot of crutches if the application or part works through the console)
Take a look at the implementation of ParamFetcher in FOSRestBundle, convert the input data into parameters for business logic at the beginning of the controller or before in the listener, and inject the already converted data further down the layers of your application. It will be ideologically more correct approach, forcing controllers to be "thin"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question