Then I got this error : Cannot resolve argument $carte of "App\Controller\CarteController::add()": Cannot autowire service "App\Classe\Carte": 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?
CarteController.php
<?php
namespace App\Controller;
use App\Entity\Car;
use App\Classe\Carte;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CarteController extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/mon-panier", name="carte")
*/
public function index(Carte $carte)
{
$carteComplete = [];
foreach($carte->get() as $id => $quantity){
$carteComplete[] = [
'car' => $this->entityManager->getRepository(Car::class)->findOneById($id),
'quantity' => $quantity
];
}
return $this->render('carte/index.html.twig', [
'carte' => $carteComplete
]);
}
/**
* @Route("/carte/add/{id}", name="add_to_carte")
*/
public function add(Carte $carte, $id)
{
$carte->add($id);
return $this->redirectToRoute("carte") ;
}
/**
* @Route("/carte/remove", name="remove_my_carte")
*/
public function remove(Carte $carte)
{
$carte->remove();
return $this->redirectToRoute("app_car");
}
}
Carte.php
<?php
namespace App\Classe;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class Carte
{
private $session;
public function __construct(SessionInterface $session){
$this->session = $session;
}
public function add($id)
{
$carte = $this->session->get('carte', []);
if(!empty($carte[$id])){
$carte[$id]++;
}else{
$carte[$id] = 1;
}
$this->session->set('carte', $carte);
}
public function get()
{
return $this->session->get('carte');
}
public function remove()
{
return $this->session->remove('carte');
}
}
Please help me solve this problem
Top comments (0)