无法自动装配服务“App\Controller\MainController”:方法“__construct()”的参数“$session”

问题描述 投票:0回答:1

有我的主控制器:

<?php

namespace App\Controller;

use App\Entity\ShopCart;
use App\Entity\ShopItem;
use App\Repository\ShopItemRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Config\Framework\SessionConfig;
use Symfony\Component\HttpFoundation\RequestStack;

class MainController extends AbstractController
{

    private SessionInterface $session;

    /**
     * @param SessionInterface $session
     */
    public function __construct(SessionInterface $session)
    {

        $this->session = $session;
        $this->session->start();
    }


    public function debug($data)
    {
        echo '<pre>' . print_r($data) . '</pre>';
    }

    #[Route('shop/list', name: 'List')]
    public function shopList(EntityManagerInterface $entityManager, ShopItemRepository $shopItemRepository): Response
    {
        $result = $shopItemRepository->findAll();
        return $this->render('shop-list.html.twig', [
            'shoes' => $result,
        ]);
    }

    /**
     * @param int $shopItem
     * @return Response
     */
    #[Route('/shop/item{id<\d+>}', name: 'shopItem')]
    public function shopItem(ShopItem $shopItem)
    {
        return $this->render('shop-item.html.twig', [
            'title' => $shopItem->getTitle(),
            'description' => $shopItem->getDescription(),
            'price' => $shopItem->getPrice(),
        ]);
    }


    #[Route('/', name: 'Home')]
    public function homepage(): Response
    {
        return $this->render('homepage.html.twig');
    }

    /**
     * @param int $shopItem
     * @return Response
     */
    #[Route('/shop/cart/add/{id<\d+>}', name: 'shopCartAdd')]
    public function shopCartAdd(ShopItem $shopItem, EntityManagerInterface $entityManager): Response
    {
        $sessionId = $this->session->getId();
        dd($sessionId);
//        $shopCart = new ShopCart();
//        $shopCart->setShopItem($shopItem);
//        $shopCart->setSessionId($sessionId);
//        $shopCart->setCount(1);

    }

    #[Route('/shop/cart', name: 'ShopCart')]
    public function shopCart(): Response
    {
        return $this->render('shopCart.html.twig');
    }

}

当我尝试 dd 时,我收到了

无法自动装配服务“App\Controller\MainController”:方法“__construct()”的参数“$session”需要“Symfony\Component\HttpFoundation\Session\SessionInterface”的实例,但这种类型已被排除在自动装配之外。

使用 Symfony 6.2.7

我尝试更改 Session 或其他服务上的 SessionInterface 但它没有帮助

php mysql symfony web shopping-cart
1个回答
1
投票

自 Symfony 5.3 起,SessionInterface 已被弃用(https://symfony.com/blog/new-in-symfony-5-3-session-service-deprecation)。

需要注入

RequestStack
并调用
getSession()
方法:

public function __construct(RequestStack $requestStack)
{
    // ...
    $session = $this->requestStack->getSession();
}

// ...
© www.soinside.com 2019 - 2024. All rights reserved.