Search code examples
symfonycart

Q: [Symfony 4] How may i access to my form's data using session from my controller?


I am building a shopping cart application using symfony4.

I have to create a shopping cart using session in order to store the data of the purchase.

I created a form that store the item selected. But i don't know how to get this item from my controller.

First, i have my twig view where i select the element :

{% extends 'base.html.twig' %}

{% block body %}
    <form action="{{ path('add_cart', {'id': product.id}) }}" method="get">
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" src="https://via.placeholder.com/350x150" alt="Card image cap">
            <div class="card-body">
                <h5 class="card-title">{{ product.name }}</h5>
                <p class="card-text">{{ product.description | raw}}</p>
                <p class="card-text">{{ product.price }} euros.</p>
                <select name="quantity_product" class="span1">
                {% for i in 1..10 %}
                    <option value= "{{ i }}">{{ i }} </option>
                {% endfor %}
                </select>
                <a href="{{path('add_cart')}}" class="btn btn-primary" name="validate_product_button">Ajouter au panier</a>
            </div>
        </div>
    </form>    
{% endblock %}

then, here is my CartController :

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;


class CartController extends AbstractController
{
    
    /**
     * @Route("/cart", name="cart")
     */
    public function index()
    {
        
        $productId = $_POST['id_product'];
        
        return $this->render('cart/index.html.twig', [
            'id_product' =>$productId
        ]);
        
    }
    /**
     * @Route("/addCart", name="add_cart")
     */

     public function addProductToCart(Request $request)
     {

        $session = new Session(new NativeSessionStorage(), new AttributeBag());

        $session->set('cart', array());

        $cart = $session->get('cart');
        
        return $this->render('cart/index.html.twig');
     }
    
}

My question is simple : how should i use the session element in order to get the element (id_product, quantity_product) from the previous form ?

I saw several answers using symfony 2 or 3. But many changes have come so far in symfony 4 and i simply don't know how to use it.


Solution

  • I'm sorry but your code doesn't respect the framework standard.

    First of all, you must use the Symfony form component instead of to write it manuely => https://symfony.com/doc/current/forms.html

    Next, it's forbidden to use the globals variables like $_POST with Symfony.

    When your code will be right, you can pass the ID Product like url's parameter to the method addProductToCart. Check the docs : https://symfony.com/doc/current/routing.html

    For the beginner : please read documentation before start a project, it's better for you !!