Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony4 "object not found by the @ParamConverter annotation" 404 error

Tags:

symfony4

I discover Symfony4 with similar blog sample like describe in https://symfony.com/doc/current/routing.html Then I added a new route to add /blog/about page. So a part of code in my src/Controller/BlogController.php is:

/**
 * @Route("/blog/{id}", name="blog_show")
 */
public function show(Description $article) {
    return $this->render('blog/show.html.twig', [
        'article' => $article,
    ]);
}

/**
 * @Route("blog/about", name="about")
 */
public function about() {
    return $this->render('blog/about.html.twig', [
        'copyright' => "GLPI 3",
    ]);
}

and when I run locahost:8000/blog/about, it returns me a 404 error :
App\Entity\Description object not found by the @ParamConverter annotation

like image 724
bcag2 Avatar asked Jul 25 '18 14:07

bcag2


3 Answers

After hours to find solution, I finally read https://symfony.com/doc/current/routing.html and understand that the /blog/{id} annotation catch /blog/about route but can't use it!

By switching functions order in my controller file:

/**
 * @Route("/blog/about", name="blog_about")
 */
public function about() {
    return $this->render('blog/about.html.twig', [
        'copyright' => "GLPI 3",
    ]);
}

/**
 * @Route("/blog/{id}", name="blog_show")
 */
public function show(Description $article) {
    return $this->render('blog/show.html.twig', [
        'article' => $article,
    ]);
}

It works fine !

The solution as mentionned by @tom is the only one with severals entities and controllers !

like image 140
bcag2 Avatar answered Oct 04 '22 18:10

bcag2


If you add a requirement to the route, then the order doesn't matter.

eg.

/**
 * @Route("/blog/{id}", name="blog_show", requirements={"id":"\d+"})
 */

The requirement is a regex.

like image 36
Tom Avatar answered Oct 04 '22 19:10

Tom


In your case you have the same Method which is GET

Therefore, the first reached path is /blog/about and "about" is interpreted as an ID which not the case !

So the easiest way to solve this is to switch functions order:

/**
 * @Route("/blog/about", name="blog_about")
 */
public function about() {
    return $this->render('blog/about.html.twig', [
        'copyright' => "GLPI 3",
    ]);
}

/**
 * @Route("/blog/{id}", name="blog_show")
 */
public function show(Description $article) {
    return $this->render('blog/show.html.twig', [
        'article' => $article,
    ]);
}
like image 40
Mustapha GHLISSI Avatar answered Oct 04 '22 18:10

Mustapha GHLISSI