DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in

Discussion on: Managing the Cart Storage |Β Building a Shopping Cart with Symfony

Collapse
 
qferrer profile image
Quentin Ferrer Author

Hello. Could you check if the session is enabled in the configuration file config/packages/framework.yaml ? You should have :

session:
        # enables the support of sessions in the app
        enabled: true
Enter fullscreen mode Exit fullscreen mode

More details on symfony.com/doc/current/session.ht...

Collapse
 
antoine__ profile image
AntoineChatry

Done! but still this message

Cannot autowire service "App\Storage\CartSessionStorage": argument "$session" of method "__construct()" references interface "Symfony\Component\HttpFoundation\Session\SessionInterface" but no such service exists. Did you create a class that implements this interface?

my framework.yaml

session:
        enabled: true
        handler_id: null
        cookie_secure: auto
        cookie_samesite: lax
        storage_factory_id: session.storage.factory.native 
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
qferrer profile image
Quentin Ferrer Author

What version of Symfony are you using?

Thread Thread
 
antoine__ profile image
AntoineChatry

Symfony 6.0

Thread Thread
 
qferrer profile image
Quentin Ferrer Author • Edited on

This tutorial was written with Symfony 5 and it's not compatible with version 6 at this time. In Symfony 6, the Session service has been removed. To get the Session, you now need to inject the RequestStack service and use the new getSession() method.

You need to upgrade the CartSessionStorage like that :

namespace App\Storage;

use App\Entity\Order;
use App\Repository\OrderRepository;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Class CartSessionStorage
 * @package App\Storage
 */
class CartSessionStorage
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * The cart repository.
     *
     * @var OrderRepository
     */
    private $cartRepository;

    /**
     * @var string
     */
    const CART_KEY_NAME = 'cart_id';

    /**
     * CartSessionStorage constructor.
     *
     * @param RequestStack $requestStack
     * @param OrderRepository $cartRepository
     */
    public function __construct(RequestStack $requestStack, OrderRepository $cartRepository)
    {
        $this->requestStack = $requestStack;
        $this->cartRepository = $cartRepository;
    }

    /**
     * Gets the cart in session.
     *
     * @return Order|null
     */
    public function getCart(): ?Order
    {
        return $this->cartRepository->findOneBy([
            'id' => $this->getCartId(),
            'status' => Order::STATUS_CART
        ]);
    }

    /**
     * Sets the cart in session.
     *
     * @param Order $cart
     */
    public function setCart(Order $cart): void
    {
        $this->requestStack->getSession()->set(self::CART_KEY_NAME, $cart->getId());
    }

    /**
     * Returns the cart id.
     *
     * @return int|null
     */
    private function getCartId(): ?int
    {
        return $this->requestStack->getSession()->get(self::CART_KEY_NAME);
    }
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
antoine__ profile image
AntoineChatry

It worked! It seems that everything is good now.
Thanks!