Answer the question
In order to leave comments, you need to log in
How to display all posts of a certain category?
How to display all posts of a certain category?
Error in this snippet
/**
* @Route("/category/{category}", name="category")
*/
public function category(Category $category)
{
return $this->render('default/category.html.twig', [
'category'=>$category
]);
<?php
namespace App\Controller;
use App\Entity\Category;
use App\Entity\Menu;
use App\Entity\Post;
use App\Entity\Tag;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/default", name="default")
*/
public function index()
{
$em = $this->getDoctrine()->getManager();
$post = $em->getRepository(Post::class)->findAll();
$menu = $em->getRepository(Menu::class)->findAll();
$category = $em->getRepository(Category::class)->findAll();
$tag = $em->getRepository(Tag::class)->findAll();
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
'post'=>$post,
'category'=>$category,
'tag'=>$tag,
'menu'=>$menu
]);
}
/**
* @Route("/post/single/{post}", name="single")
*/
public function single(Post $post)
{
return $this->render('default/single.html.twig', [
'post'=>$post
]);
}
/**
* @Route("/category/{category}", name="category")
*/
public function category(Category $category)
{
return $this->render('default/category.html.twig', [
'category'=>$category
]);
}
}
Answer the question
In order to leave comments, you need to log in
Something like this if the Post has a category property
/**
* @Route("/category/{category}", name="category")
*/
public function category(Category $category)
{
return $this->render('default/category.html.twig', [
'category' => $category,
'posts' => $this->getDoctrine()->getManager()->getRepository(Post::class)->findBy(['category' => $category])
]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question