postQuery = $postQuery; } /** * @UrlGenerator(service=PostGenerator::class, method="post") */ public function post( Post $post, string $slug, CommentFactory $commentFactory, Request $request, EntityManager $entityManager ): Response { if (Post::DRAFT === $post->getStatus() && !$this->getUser()) { throw $this->createNotFoundException(); } if ($slug !== $post->getSlug()) { return $this->redirectToRoute( $this->siteRequest->getNode()->getRouteName(), [ 'post' => $post->getId(), 'slug' => $post->getSlug(), ] ); } $form = $this->createForm(UserCommentType::class, $commentFactory->create($post)); if ($request->isMethod('POST')) { $form->handleRequest($request); if ($form->isValid()) { $data = $request->request->get($form->getName()); $parentCommentId = (int) $data['parentCommentId']; foreach ($post->getComments(['id' => $parentCommentId]) as $comment) { $form->getData()->setParentComment($comment); } $entityManager->create($form->getData()); $this->addFlash('success', 'Commentaire publiƩ !'); return $this->redirect($request->getUri()); } $this->addFlash('error', 'Le formulaire n\'est pas valide.'); } return $this->defaultRender('blog/post/post.html.twig', [ 'post' => $post, 'form' => $form->createView(), ]); } public function posts(int $page = 1): Response { $entities = $this->createQuery() ->paginate($page, 5) ; return $this->defaultRender('blog/post/posts.html.twig', [ 'pager' => $entities, ]); } public function search(Request $request, int $page = 1): Response { $query = $request->query->get('query'); if ($query) { $entities = $this->createQuery() ->search($query) ->paginate($page, 5) ; } return $this->defaultRender('blog/post/search.html.twig', [ 'pager' => $entities ?? null, 'query' => $query, ]); } /** * @UrlGenerator(service=PostGenerator::class, method="category") */ public function category(Category $category, string $slug, int $page = 1): Response { $entities = $this->createQuery() ->inCategory($category) ->paginate($page, 5) ; if (!$category->getIsActive() && !$this->getUser()) { throw $this->createNotFoundException(); } if ($slug !== $category->getSlug()) { return $this->redirectToRoute( $this->siteRequest->getNode()->getRouteName(), [ 'category' => $category->getId(), 'slug' => $category->getSlug(), ] ); } return $this->defaultRender('blog/post/category.html.twig', [ 'category' => $category, 'pager' => $entities, ]); } public function tag(string $tag, int $page = 1): Response { } public function createQuery(): PostRepositoryQuery { return $this->postQuery->create() ->orderBy('.publishedAt', 'DESC') ->published() ; } }