Victor Bocharsky
Victor Bocharsky

Reputation: 12306

how can I set content of request symfony?

I generate manually request content of my form and want to set data like:

form[1][]=2&form[2][]=3&form[3][]=5&form[4][]=8&form[apply]=

The Symfony Request object has an getContent() method, but hasn't setContent().

How can I set content alternatively?

Upvotes: 12

Views: 8649

Answers (4)

Volmarg Reiso
Volmarg Reiso

Reputation: 483

Another dirty solution with usage of closures: https://www.php.net/manual/en/closure.call.php

$requestClosure = function() use ($decryptedJson) {
    $this->content = $decryptedJson;
    return $this;
};
$request = $requestClosure->call($request);

How this works:

  • in closure the $this becomes the request itself so it's possible to call private content,
  • calling return $this in closure returns the $request in context of which the call was made,

Upvotes: 0

Luke Duda
Luke Duda

Reputation: 934

Symfony 4 > version:

$request->initialize(
    $request->query->all(),
    $request->request->all(),
    $request->attributes->all(),
    $request->cookies->all(),
    $request->files->all(),
    $request->server->all(),
    'your-content>'
);

Upvotes: 6

Aldee
Aldee

Reputation: 4518

Another option is via Request::create method.

Request::create(
   '<uri>', 
   '<method>', 
   <parameters>, 
   <cookies>, 
   <files>, 
   <server>, 
   '<your-content>'
);

Source: HTTP foundation

Upvotes: 2

sebbo
sebbo

Reputation: 2929

Found a solution which is dirty but seems to work for me. Just reinitialize the request.

    $request->initialize(
        $request->query,
        $request->request,
        $request->attributes,
        $request->cookies,
        $request->files,
        $request->server,
        '<your-content>'
    );

Upvotes: 9

Related Questions