This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
BlogController.php
130 lines (114 loc) · 4.16 KB
/
BlogController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Controller;
use AppBundle\Entity\Comment;
use AppBundle\Entity\Post;
use AppBundle\Form\CommentType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller used to manage blog contents in the public part of the site.
*
* @Route("/blog")
*
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class BlogController extends Controller
{
/**
* @Route("/", name="blog_index")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$posts = $em->getRepository('AppBundle:Post')->findLatest();
return $this->render('blog/index.html.twig', array('posts' => $posts));
}
/**
* @Route("/posts/{slug}", name="blog_post")
*
* NOTE: The $post controller argument is automatically injected by Symfony
* after performing a database query looking for a Post with the 'slug'
* value given in the route.
* See http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
*/
public function postShowAction(Post $post)
{
return $this->render('blog/post_show.html.twig', array('post' => $post));
}
/**
* @Route("/comment/{postSlug}/new", name = "comment_new")
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
*
* @Method("POST")
* @ParamConverter("post", options={"mapping": {"postSlug": "slug"}})
*
* NOTE: The ParamConverter mapping is required because the route parameter
* (postSlug) doesn't match any of the Doctrine entity properties (slug).
* See http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
*/
public function commentNewAction(Request $request, Post $post)
{
$form = $this->createCommentForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var Comment $comment */
$comment = $form->getData();
$comment->setAuthorEmail($this->getUser()->getEmail());
$comment->setPost($post);
$comment->setPublishedAt(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
return $this->redirectToRoute('blog_post', array('slug' => $post->getSlug()));
}
return $this->render('blog/comment_form_error.html.twig', array(
'post' => $post,
'form' => $form->createView(),
));
}
/**
* This controller is called directly via the render() function in the
* blog/post_show.html.twig template. That's why it's not needed to define
* a route name for it.
*
* The "id" of the Post is passed in and then turned into a Post object
* automatically by the ParamConverter.
*
* @param Post $post
*
* @return Response
*/
public function commentFormAction(Post $post)
{
$form = $this->createCommentForm();
return $this->render('blog/comment_form.html.twig', array(
'post' => $post,
'form' => $form->createView(),
));
}
/**
* This is a utility method used to create comment forms. It's recommended
* to not define this kind of methods in a controller class, but sometimes
* is convenient for defining small methods.
*/
private function createCommentForm()
{
$form = $this->createForm(new CommentType());
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
}