不能使用 Symfony\Component\HttpFoundation\Session\Session 类型的对象作为数组 (Symfony 5.3.9)
Cannot use object of type Symfony\Component\HttpFoundation\Session\Session as array (Symfony 5.3.9)
我正在尝试将 Symfony 版本 5.3.9 中的会话与 RequestStack 一起使用,因为 SessionInterface 已被弃用。
我收到以下错误:
Cannot use object of type Symfony\Component\HttpFoundation\Session\Session as array
此处:
if(isset($cart[$id])){ (in my addToCart function)
在 symfony 5.2 中没问题
感谢您的帮助
我的CartController.php:
<?php
namespace App\Controller;
use App\Services\CartServices;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class CartController extends AbstractController
{
/**
* @Route("/cart", name="cart")
*/
public function index(CartServices $cartServices): Response
{
$cartServices->addToCart(3);
dd($cartServices->getCart());
return $this->render('cart/index.html.twig', [
'controller_name' => 'CartController',
]);
}
}
我的CartServices.php:
<?php
namespace App\Services;
use App\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\RequestStack;
class CartServices
{
private $requestStack;
private $repoProduct;
public function __construct(RequestStack $requestStack, ProductRepository $repoProduct)
{
$this->requestStack = $requestStack;
$this->repoProduct = $repoProduct;
}
public function addToCart($id){
$cart = $this->getCart();
if(isset($cart[$id])){
$cart[$id]++;
}else{
$cart[$id] = 1;
}
$this->updateCart($cart);
}
$cart = $this->getCart():
public function getCart(){
return $this->requestStack->getSession('cart', []);
}
非常感谢,但我仍然没有结果
我的CartServices.php
<?php
namespace App\Services;
use App\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\RequestStack;
class CartServices
{
private $requestStack;
private $repoProduct;
public function __construct(RequestStack $requestStack, ProductRepository $repoProduct)
{
$this->requestStack = $requestStack;
$this->repoProduct = $repoProduct;
}
public function addToCart($id){
$cart = $this->getCart();
if(isset($cart[$id])){
//produit déjà dans le panier on incrémente
$cart[$id]++;
}else{
//produit pas encore dans le panier on ajoute
$cart[$id] = 1;
}
$this->updateCart($cart);
}
public function deleteFromCart($id){
$cart = $this->getCart();
//si produit déjà dans le panier
if(isset($cart[$id])){
//si il y a plus d'une fois le produit dans le panier on décrémente
if($cart[$id] >1){
$cart[$id] --;
}else{
//Sinon on supprime
unset($cart[$id]);
}
//on met à jour la session
$this->updateCart($cart);
}
}
public function deleteAllToCart($id){
$cart = $this->getCart();
//si produit(s) déjà dans le panier
if(isset($cart[$id])){
//on supprime
unset($cart[$id]);
}
//on met à jour la session
$this->updateCart($cart);
}
public function deleteCart(){
//on supprime tous les produits (on vide le panier)
$this->updateCart([]);
}
public function updateCart($cart){
$this->requestStack->getSession('cart', $cart);
}
public function getCart(){
$session = $this->requestStack->getSession();
return $session->get('cart', []);
}
public function getFullCart(){
$cart = $this->getCart();
$fullCart = [];
foreach ($cart as $id => $quantity){
$product = $this->repoProduct->find($id);
if($product){
//produit récupéré avec succés
$fullCart[]=[
'quantity' => $quantity,
'product' => $product
];
}else{
//id incorrect
$this->deleteFromCart($id); //on ne met pas à jour la session car cette method le fait aussi (voir plus haut dans la fonction deleteFromCart)
}
}
}
}
我的CartController.php
<?php
namespace App\Controller;
use App\Services\CartServices;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CartController extends AbstractController
{
/**
* @Route("/cart/add/{id}")
*/
public function addToCart($id,CartServices $cartServices):Response
{
$cartServices->addToCart($id);
dd($cartServices->getCart(1));
return $this->render('cart/index.html.twig', [
'controller_name' => 'CartController',
]);
}
}
public function getCart(){
return $this->requestStack->getSession('cart', []);
}
RequestStack
return 的方法 getSession
是 SessionInterface
的对象,所以你的代码不正确,下面是方法的主体:
/**
* Gets the current session.
*
* @throws SessionNotFoundException
*/
public function getSession(): SessionInterface
{
if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
return $request->getSession();
}
throw new SessionNotFoundException();
}
所以,您应该像这样更新您的方法 getCart
:
public function getCart(){
$session = $this->requestStack->getSession();
return $session->get('cart', []);
}
我正在尝试将 Symfony 版本 5.3.9 中的会话与 RequestStack 一起使用,因为 SessionInterface 已被弃用。 我收到以下错误:
Cannot use object of type Symfony\Component\HttpFoundation\Session\Session as array
此处:
if(isset($cart[$id])){ (in my addToCart function)
在 symfony 5.2 中没问题
感谢您的帮助
我的CartController.php:
<?php
namespace App\Controller;
use App\Services\CartServices;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class CartController extends AbstractController
{
/**
* @Route("/cart", name="cart")
*/
public function index(CartServices $cartServices): Response
{
$cartServices->addToCart(3);
dd($cartServices->getCart());
return $this->render('cart/index.html.twig', [
'controller_name' => 'CartController',
]);
}
}
我的CartServices.php:
<?php
namespace App\Services;
use App\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\RequestStack;
class CartServices
{
private $requestStack;
private $repoProduct;
public function __construct(RequestStack $requestStack, ProductRepository $repoProduct)
{
$this->requestStack = $requestStack;
$this->repoProduct = $repoProduct;
}
public function addToCart($id){
$cart = $this->getCart();
if(isset($cart[$id])){
$cart[$id]++;
}else{
$cart[$id] = 1;
}
$this->updateCart($cart);
}
$cart = $this->getCart():
public function getCart(){
return $this->requestStack->getSession('cart', []);
}
非常感谢,但我仍然没有结果
我的CartServices.php
<?php
namespace App\Services;
use App\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\RequestStack;
class CartServices
{
private $requestStack;
private $repoProduct;
public function __construct(RequestStack $requestStack, ProductRepository $repoProduct)
{
$this->requestStack = $requestStack;
$this->repoProduct = $repoProduct;
}
public function addToCart($id){
$cart = $this->getCart();
if(isset($cart[$id])){
//produit déjà dans le panier on incrémente
$cart[$id]++;
}else{
//produit pas encore dans le panier on ajoute
$cart[$id] = 1;
}
$this->updateCart($cart);
}
public function deleteFromCart($id){
$cart = $this->getCart();
//si produit déjà dans le panier
if(isset($cart[$id])){
//si il y a plus d'une fois le produit dans le panier on décrémente
if($cart[$id] >1){
$cart[$id] --;
}else{
//Sinon on supprime
unset($cart[$id]);
}
//on met à jour la session
$this->updateCart($cart);
}
}
public function deleteAllToCart($id){
$cart = $this->getCart();
//si produit(s) déjà dans le panier
if(isset($cart[$id])){
//on supprime
unset($cart[$id]);
}
//on met à jour la session
$this->updateCart($cart);
}
public function deleteCart(){
//on supprime tous les produits (on vide le panier)
$this->updateCart([]);
}
public function updateCart($cart){
$this->requestStack->getSession('cart', $cart);
}
public function getCart(){
$session = $this->requestStack->getSession();
return $session->get('cart', []);
}
public function getFullCart(){
$cart = $this->getCart();
$fullCart = [];
foreach ($cart as $id => $quantity){
$product = $this->repoProduct->find($id);
if($product){
//produit récupéré avec succés
$fullCart[]=[
'quantity' => $quantity,
'product' => $product
];
}else{
//id incorrect
$this->deleteFromCart($id); //on ne met pas à jour la session car cette method le fait aussi (voir plus haut dans la fonction deleteFromCart)
}
}
}
}
我的CartController.php
<?php
namespace App\Controller;
use App\Services\CartServices;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CartController extends AbstractController
{
/**
* @Route("/cart/add/{id}")
*/
public function addToCart($id,CartServices $cartServices):Response
{
$cartServices->addToCart($id);
dd($cartServices->getCart(1));
return $this->render('cart/index.html.twig', [
'controller_name' => 'CartController',
]);
}
}
public function getCart(){
return $this->requestStack->getSession('cart', []);
}
RequestStack
return 的方法 getSession
是 SessionInterface
的对象,所以你的代码不正确,下面是方法的主体:
/**
* Gets the current session.
*
* @throws SessionNotFoundException
*/
public function getSession(): SessionInterface
{
if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
return $request->getSession();
}
throw new SessionNotFoundException();
}
所以,您应该像这样更新您的方法 getCart
:
public function getCart(){
$session = $this->requestStack->getSession();
return $session->get('cart', []);
}