Erreur lorque j'essaye de voir le details d'un article apartir du slug

Par Corail Dos Santos, il y a 4 ans


Bonjour à tous j'ai cette erreur lorsque je j'essaye de voir le details d'un article

Cannot autowire argument $article of "App\Controller\BlogController::view()": it references class "App\Entity\Article" but no such service exists.

5 réponses

Arbi, il y a 4 ans
/** * @Route("/article/{id}-{slug}", name="article_view") * */ public function view($id,$slug,ArticleRepository $articleRepository) { $article = $articleRepository->findOneById($id); return $this->render('article/view.html.twig', compact('article')); }

il faut pas oublier les import

use App\Repository\ArticleRepository;
gillesr, il y a 4 ans

Bonjour,

Tu peux même simplifier en utilisant le paramConverter de symfony :

use App\Entity\Article; /** * @Route("/article/{slug}", name="article_view") * */ public function view(Article $article) { return $this->render('article/view.html.twig', compact('article')); }
Corail Dos Santos, il y a 4 ans

Merci bien mais le soucis c'est le controller j'essaye de affiche les commentaires et le détails d'un article sur la meme page mais lorsque je passe l'article en parametre pour faire faire persisté avec le commentaire en question j'ai cette erreur
Cannot autowire argument $article of "App\Controller\BlogController::view()": it references class "App\Entity\Article" but no such service exists.

Arbi, il y a 4 ans

si vous pouvez mettre le code de votre methode ?

Corail Dos Santos, il y a 4 ans

Bonjour voici le code

/**

  • @Route("/view/{id}",name="view")
  • @Security("is_granted('ROLE_USER')")
  • @param Article $article
  • @return Response
    */
    public function view(Article $article, Request $request, EntityManagerInterface $manager,ArticleRepository $articleRepository){
    $comment= new Comment();
    $form=$this->createForm(CommentType::class, $comment);
    $form->handleRequest($request);
    if($form->isSubmitted() && $form->isValid()){
    $comment->setCreatedAt(new \DateTime())
    ->setArticle($article);
    $comment->setAuthor($this->getUser()->getUsername());
    $manager->persist($comment);
    $manager->flush();

              return $this->redirectToRoute('view',[
                'id'=>$article->getId()
              ]);
    }
    
    return $this->render('blog/view.html.twig', [
        'article' => $article,
        'commentForm'=> $form->createView(), 
        'articlees'=>$articleRepository->findArticleRecent()          
    ]);