Put arrays in symfony2 session

I have the following code:

 public function addAction(Request $request){
    $aBasket[] = $request->request->all();
    $this->get('session')->set('aBasket', $aBasket);
    return $this->redirect($this->generateUrl('shop_desktop_homepage'));
    print_r($aBasket);
}

      

Works fine, but only the last array that was saved is saved in the session. How to enable a session. For example, the array is stored like this:

array:1 [▼
    0 => array:3 [▶]
]

      

But I want to keep:

array:1 [▼
  0 => array:3 [▶]
  1 => array:3 [▶]
  2 => array:3 [▶]
]

      

not only the last one. Please help me!! thanks in advance.

+3


source to share


1 answer


I have not tested the code. But it will help.

public function addAction(Request $request){

  $aBasket = $request->request->all();
  // Get Value from session
  $sessionVal = $this->get('session')->get('aBasket');
  // Append value to retrieved array.
  $sessionVal[] = $aBasket;
  // Set value back to session
  $this->get('session')->set('aBasket', $sessionVal);
  return $this->redirect($this->generateUrl('shop_desktop_homepage'));

}

      



I wrote a comment. I have not checked the return value with the session get method. you need to check the return type from the get session.

Hope for this help.

+5


source







All Articles