I'm trying to store some data in the session in my Symfony application, but it seems like the session data is not persisting across requests. Can someone help me figure out what I'm doing wrong?this is my controller for showing the product en the session
<?phpnamespace App\Controller;use App\Entity\Product;use App\Entity\Purchase;use App\Entity\PurchaseLine;use Doctrine\ORM\EntityManagerInterface;use phpDocumentor\Reflection\Types\This;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\Form\Extension\Core\Type\IntegerType;use Symfony\Component\Form\Extension\Core\Type\SubmitType;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Attribute\Route;class MemberController extends AbstractController{ #[Route('/member', name: 'app_member')] public function index(): Response { return $this->render('member/index.html.twig', ['controller_name' => 'MemberController', ]); } #[Route('/products', name: 'products')] public function products(EntityManagerInterface $em): Response { $products = $em->getRepository(Product::class)->findAll(); return $this->render('member/products.html.twig', ['products' => $products ]); } #[Route('/makeorder/{id}', name: 'makeorder')] public function makeOrder(EntityManagerInterface $em, Request $request, int $id): Response { $product = $em->getRepository(Product::class)->find($id); $form = $this->createFormBuilder() ->add('amount', IntegerType::class, ['required' => true,'data' => 1,'label' => 'aantal' ]) ->add('save', SubmitType::class) ->getForm(); $form->handleRequest($request); if ($form->isSubmitted()) { $session = $request->getSession(); if (!$session->get('order')) { $session->set('order', []); } $amount = $form->get('amount')->getData(); // het opvragen van de ingevulde producten uit het form $order = $session->get('order'); // het ophalen van huidige bestellingen uit de sessie. $order[] = [$id, $amount]; // het toevoegen van het huidige product en de bijbehorende hoeveelheid aan de bestellingen $session->set('order', $order); // het bijwerken van de bestelling in de sessie met de nieuwe toevoeging. $this->addFlash('success', 'het product is toegevoegd'); return $this->redirectToRoute('products'); } return $this->render('member/order.html.twig', ['product' => $product,'form' => $form ]); } #[Route('/show', name: 'show_products')] public function showProducts(EntityManagerInterface $em, Request $request): Response { $order = $request->getSession()->get('order'); if ($order === null) { $this->addFlash('danger', 'winelwagen is leeg'); return $this->redirectToRoute('products'); } $purchaseLines = []; foreach ($order as $orderLine) { $purchaseLine = new PurchaseLine(); $product = $em->getRepository(Product::class)->find($orderLine[0]); $purchaseLine->setProduct($product); $purchaseLine->setAmount($orderLine[1]); $purchaseLines[] = $purchaseLine; } return $this->render('member/show.html.twig', ['purchaselines' => $purchaseLines ]); } #[Route('/clear/products', name: 'clear')] public function clear(EntityManagerInterface $em, Request $request): Response { $request->getSession()->remove('order'); return $this->redirectToRoute('products'); } #[Route('/save/products', name: 'save')] public function saveOrder(EntityManagerInterface $em, Request $request): Response { $p=$request->getSession()->get('order'); if (!$p) { $this->addFlash('danger', 'er is geen order'); return $this->redirectToRoute('products'); } $order = new Purchase(); $order->setDate(new \DateTime('now')); foreach ($p as $line) { $orderLine=new PurchaseLine(); $product=$em->getRepository(Product::class)->find($line[0]); $orderLine->setProduct($product); $orderLine->setAmount($line[1]); $orderLine->setPurchase($order); $em->persist($orderLine); } $em->persist($order); $em->flush(); $request->getSession('order')->clear(); $this->addFlash('success', 'de bestelling is voltooid'); return $this->redirectToRoute('products'); }}
these are my enity's
<?phpnamespace App\Entity;use App\Repository\ProductRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\DBAL\Types\Types;use Doctrine\ORM\Mapping as ORM;#[ORM\Entity(repositoryClass: ProductRepository::class)]class Product{ #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column(length: 255)] private ?string $name = null; #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)] private ?string $price = null; #[ORM\Column(length: 255)] private ?string $image = null; #[ORM\Column(type: Types::TEXT, nullable: true)] private ?string $description = null; /** * @var Collection<int, PurchaseLine> */ #[ORM\OneToMany(targetEntity: PurchaseLine::class, mappedBy: 'product', orphanRemoval: true)] private Collection $purchaseLines; public function __construct() { $this->purchaseLines = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getName(): ?string { return $this->name; } public function setName(string $name): static { $this->name = $name; return $this; } public function getPrice(): ?string { return $this->price; } public function setPrice(string $price): static { $this->price = $price; return $this; } public function getImage(): ?string { return $this->image; } public function setImage(string $image): static { $this->image = $image; return $this; } public function getDescription(): ?string { return $this->description; } public function setDescription(?string $description): static { $this->description = $description; return $this; } /** * @return Collection<int, PurchaseLine> */ public function getPurchaseLines(): Collection { return $this->purchaseLines; } public function addPurchaseLine(PurchaseLine $purchaseLine): static { if (!$this->purchaseLines->contains($purchaseLine)) { $this->purchaseLines->add($purchaseLine); $purchaseLine->setProduct($this); } return $this; } public function removePurchaseLine(PurchaseLine $purchaseLine): static { if ($this->purchaseLines->removeElement($purchaseLine)) { // set the owning side to null (unless already changed) if ($purchaseLine->getProduct() === $this) { $purchaseLine->setProduct(null); } } return $this; }}
<?phpnamespace App\Entity;use App\Repository\PurchaseRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\DBAL\Types\Types;use Doctrine\ORM\Mapping as ORM;#[ORM\Entity(repositoryClass: PurchaseRepository::class)]class Purchase{ #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column(type: Types::DATETIME_MUTABLE)] private ?\DateTimeInterface $date = null; /** * @var Collection<int, PurchaseLine> */ #[ORM\OneToMany(targetEntity: PurchaseLine::class, mappedBy: 'purchase', orphanRemoval: true)] private Collection $purchaseLines; public function __construct() { $this->purchaseLines = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getDate(): ?\DateTimeInterface { return $this->date; } public function setDate(\DateTimeInterface $date): static { $this->date = $date; return $this; } /** * @return Collection<int, PurchaseLine> */ public function getPurchaseLines(): Collection { return $this->purchaseLines; } public function addPurchaseLine(PurchaseLine $purchaseLine): static { if (!$this->purchaseLines->contains($purchaseLine)) { $this->purchaseLines->add($purchaseLine); $purchaseLine->setPurchase($this); } return $this; } public function removePurchaseLine(PurchaseLine $purchaseLine): static { if ($this->purchaseLines->removeElement($purchaseLine)) { // set the owning side to null (unless already changed) if ($purchaseLine->getPurchase() === $this) { $purchaseLine->setPurchase(null); } } return $this; }}
<?phpnamespace App\Entity;use App\Repository\PurchaseLineRepository;use Doctrine\ORM\Mapping as ORM;#[ORM\Entity(repositoryClass: PurchaseLineRepository::class)]class PurchaseLine{ #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column] private ?int $amount = null; #[ORM\ManyToOne(inversedBy: 'purchaseLines')] #[ORM\JoinColumn(nullable: false)] private ?product $product = null; #[ORM\ManyToOne(inversedBy: 'purchaseLines')] #[ORM\JoinColumn(nullable: false)] private ?Purchase $purchase = null; public function getId(): ?int { return $this->id; } public function getAmount(): ?int { return $this->amount; } public function setAmount(int $amount): static { $this->amount = $amount; return $this; } public function getProduct(): ?product { return $this->product; } public function setProduct(?product $product): static { $this->product = $product; return $this; } public function getPurchase(): ?Purchase { return $this->purchase; } public function setPurchase(?Purchase $purchase): static { $this->purchase = $purchase; return $this; }}
this is my twig fileproduct
{% for p in products %}<div class="card" style="width: 18rem"><img src="{{ asset(p.image) }}" class="card-img-top"><div class="card-body"><h5 class="card-title">{{ p.name }}</h5><p class="card-text">{{ p.description }}</p><a href="{{ path('makeorder',{id:p.id}) }}" class="btn btn-primary">order</a></div></div> {% endfor %}<a href="{{ path('show_products')}}" class="btn btn-primary">winkelwagen</a>
order
<img src="{{ asset(product.image) }}" class="card-img-top" alt="..."><div class="card-body"><h5 class="card-title">{{ product.name }}</h5><p class="card-text">{{ product.description }}</p> {{ form(form) }} </div>
show
{% for p in purchaselines %}<div class="card" style="width: 18rem;"><img src="{{ asset(p.product.image) }}" class="card-img-top" alt="..."><div class="card-body"><h5 class="card-title">{{ p.product.name }}</h5><p class="card-text">{{ p.product.description }}</p><h2>{{ p.amount}}</h2></div></div> {% endfor %}<a class="btn btn-warning" href="{{ path('clear')}}">clear</a><a class="btn btn-success" href="{{ path('save')}}">order</a>
I checked my PHP session configuration to ensure sessions are enabled.I verified that the session directory is writable.I cleared the Symfony cache using php bin/console cache:clear.I tried using $request->getSession() instead of injecting SessionInterface.