Exemplo n.º 1
0
 /**
  * @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];
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     $blogArticleRecords = [];
     for ($i = 0; $i < 10; ++$i) {
         $blogArticleRecords[] = ['title' => 'Article #' . ($i + 1), 'description' => 'Description of Article #' . ($i + 1), 'content' => 'Content of Article #' . ($i + 1)];
     }
     foreach ($blogArticleRecords as $blogArticleRecord) {
         $blogArticle = new BlogArticle();
         $blogArticle->setTitle($blogArticleRecord['title']);
         $blogArticle->setDescription($blogArticleRecord['description']);
         $blogArticle->setContent($blogArticleRecord['content']);
         $blogArticle->setImageUrl('');
         $blogArticle->setPublicationDate(new \DateTime());
         $manager->persist($blogArticle);
         $manager->flush();
         $this->referenceRepository->addReference('blog-article-' . $blogArticle->getId(), $blogArticle);
     }
 }
Exemplo n.º 4
0
 /**
  * @test
  */
 public function getAllPostsSuccess()
 {
     $blogs = new ArrayCollection();
     $blog_article = new BlogArticle();
     $blog_article->setId(1);
     $blog_article->setTitle('test');
     $blog_article->setContent('content');
     $blog_article->setCategory($this->createCategory(1));
     $blogs->add($blog_article);
     $blog_article = new BlogArticle();
     $blog_article->setId(2);
     $blog_article->setTitle('test_2');
     $blog_article->setContent('content_2');
     $blog_article->setCategory($this->createCategory(1));
     $blogs->add($blog_article);
     $this->repository->expects($this->once())->method('findAll')->will($this->returnValue($blogs));
     $posts = $this->blog_service->getAllPosts();
     $post = $posts->first();
     $this->assertCount(2, $posts->toArray());
     $this->assertInstanceOf('AppBundle\\Entity\\BlogArticle', $post);
     $this->assertEquals(1, $post->getId());
 }