I tried this in my program sessions Basic Usage. I do it like this:
framwork.yml
session:
handler_id: 'session.handler.native_file'
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
cookie_secure: auto
cookie_samesite: lax
storage_factory_id: session.storage.factory.native
and I use a service to set and get the data from the Session.
class SessionService
{
private $requestStack;
public const PRODUCT = 'product';
public const AMOUNT = 'amount';
private const SHOPPIN_CART= 'shopping_cart_products';
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function setShoppingCart( Product $product, int $amount)
{
$shoppingCart = $this->getShoppingCart();
if ($shoppingCart == null){
$shoppingCart[0][self::PRODUCT] = $product;
$shoppingCart[0][self::AMOUNT] = $amount;
}else{
$id = count($shoppingCart)-1;
$shoppingCart[$id][self::PRODUCT] = $prod;
$shoppingCart[$id][self::AMOUNT] = $amount;
}
$session = $this->requestStack->getSession();
$session->set(self::SHOPPIN_CART, $shoppingCart);
}
public function getShoppingCart():array
{
$session = $this->requestStack->getSession();
$shoppingCart = $session->get(self::SHOPPIN_CART);
if($shoppingCart == null){
return [];
}
return $shoppingCart;
}}
the problem is that the data arn't saved in the session or getting lost after the reload of the Side and I don't know why. Thanks for your help.