I have read about Namespaced Attributes. I try to use this feature :
#src/Controller/CartController.php
public function addProduct(int $cart_id, int $product_id, Request $request, SessionManager $sessionManager)
{
$session = new Session(new NativeSessionStorage(), new NamespacedAttributeBag());
$session->set("cart_$cart_id/product_$product_id", $request->get('selected_product_qty'));
dump($session);
die();
}
It work well.
So as an improvement, I want to override the session services, so it'll use namespacedattributebag everywhere and it use Depency Injection instead of coupling Session service into addProduct function.
To do this I edit my services.yaml
#config/services.yaml
session:
public: true
class: Symfony\Component\HttpFoundation\Session\Session
arguments: ['@session.storage', '@session.namespacedattributebag']
session.namespacedattributebag:
class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
And then I update CartController :
public function addProduct(int $cart_id, int $product_id, Request $request, Session $session)
{
$session->set("cart_$cart_id/product_$product_id", $request->get('selected_product_qty'));
dump($session);
die();
}
Now I got the following error message :
Circular reference detected for service "session", path: "session -> session.flash_bag -> session".
This error should have been solved in this issue : #36261 It have been discussed here.
But for me the problem still present... (I am using Symfony 5.1)