/** * @Route("comment/{id}") * @ParamConverter("blog", class="AppBundle:BlogArticle") * @Method("post") */ public function createAction(Request $request, BlogArticle $blog) { $comment = new Comment($blog); $form = $this->form_factory->create('comment', $comment); $form->handleRequest($request); if ($form->isValid()) { $this->service->add($comment); return $this->redirect($this->generateUrl('app_blog_show', ['id' => $blog->getId()])); } return $this->render('Comment/form.html.twig', ['form' => $form->createView(), 'comment' => $comment]); }
/** * @Method({"GET"}) * @Route("/{id}/read/{slug}", name="app_blog_article_read", requirements={"id" = "\d+", "slug" = "[a-z0-9_-]+"}, defaults={"slug" = null}) * @Template("blog/article/read.html.twig") */ public function readAction(Request $request, BlogArticle $article, $slug = null) { if ($article->getPublicationDate() == null) { throw new NotFoundHttpException(); } /** @var Slugify $slugify */ $slugify = $this->get('slugify'); $slugCheck = $slugify->slugify($article->getTitle()); if ($slug !== $slugCheck) { return $this->redirect($this->generateUrl('app_blog_article_read', ['id' => $article->getId(), 'slug' => $slugCheck])); } $articlesRead = $request->getSession()->get('articles_read', []); if (!in_array($article->getId(), $articlesRead)) { $article->setViews($article->getViews() + 1); $this->getDoctrine()->getManager()->persist($article); $this->getDoctrine()->getManager()->flush(); $articlesRead[] = $article->getId(); } $request->getSession()->set('articles_read', $articlesRead); return ['article' => $article]; }