alexandernst
alexandernst

Reputation: 15109

Save a request in a session

I have the following code inside a controller:

$request = $this->getRequest();
$session = $request->getSession();

I'm POSTing some data to that controller (from a form). I want to save the posted data in a session variable. Is that possible?

Maybe serializing the request object? If this is the correct way, how would I serialize it?

Upvotes: 2

Views: 1078

Answers (1)

sjagr
sjagr

Reputation: 16502

Similar to how you can get all of your session values into an array using $session->all(), you can fetch all of the request values using $request->request->all(), so your final product would be:

$session->set('postData', $request->request->all());

To get the data back:

$postDataFromBefore = $session->get('postData');

And you access the values of $postDataFromBefore like you would access any traditional $_POST array since Symfony preserves the session data in the same data type. So $_POST['my_value'] would translate right to $postDataFromBefore['my_value'].

Upvotes: 3

Related Questions