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

Anf*_*Fun 0 php mysql shopping-cart symfony web

有我的主控制器:

<?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');
    }

}

Run Code Online (Sandbox Code Playgroud)

当我尝试 dd 时我收到

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

使用 Symfony 6.2.7

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

Alb*_*chi 5

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

您需要注入RequestStack并调用getSession()方法:

use Symfony\Component\HttpFoundation\RequestStack;

class SomeService
{
    private $requestStack;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function someMethod()
    {
        $session = $this->requestStack->getSession();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我会警告不要在构造函数中访问会话,因为可能存在计时问题。实例化控制器时,会话可能不可用。需要时拉动它更安全。事实上,由于它是一个控制器,所以最好的解决方案是将 $request 对象注入到操作方法中并从中提取会话。 (6认同)