src/Controller/BlogController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Comment;
  4. use App\Entity\Post;
  5. use App\Services\PostService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class BlogController extends AbstractController
  12. {
  13.     #[Route('/blog'name'app_blog')]
  14.     public function showBlog(EntityManagerInterface $entityManager): Response
  15.     {
  16.         $posts $entityManager->getRepository(Post::class)->findBy([], ['id' => 'DESC']);
  17.         return $this->render('blog/index.html.twig', [
  18.             'posts' => $posts,
  19.         ]);
  20.     }
  21.     #[Route('/blog/singlePost/{postId}'name'app_blog_post_single')]
  22.     public function showSinglePost(EntityManagerInterface $entityManagerstring $postId)
  23.     {
  24.         $post $entityManager->getRepository(Post::class)->find($postId);
  25.         $comments $entityManager->getRepository(Comment::class)->findBy(['post' => $postId]);
  26.         return $this->render('blog/single_post.html.twig', [
  27.             'post' => $post,
  28.             'comments' => $comments
  29.         ]);
  30.     }
  31.     #[Route('/blog/addNewPost'name'app_blog_post_add')]
  32.     public function addNewPost(EntityManagerInterface $entityManagerRequest $request)
  33.     {
  34.         $postService = new PostService();
  35.         $postService->addNewPost($entityManager$request);
  36.         return $this->render('blog/add_post.html.twig');
  37.     }
  38.     #[Route('/blog/addComment'name'app_blog_comment_add')]
  39.     public function addComment(EntityManagerInterface $entityManagerRequest $request)
  40.     {
  41.         $postService = new PostService();
  42.         $postService->addComment($entityManager$request);
  43.         return new Response(''Response::HTTP_OK);
  44.     }
  45. }