/
BlogController.php
153 lines (135 loc) · 6.06 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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\Events;
use AppBundle\Form\CommentType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
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\EventDispatcher\GenericEvent;
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("/", defaults={"page": "1", "_format"="html"}, name="blog_index")
* @Route("/rss.xml", defaults={"page": "1", "_format"="xml"}, name="blog_rss")
* @Route("/page/{page}", defaults={"_format"="html"}, requirements={"page": "[1-9]\d*"}, name="blog_index_paginated")
* @Method("GET")
* @Cache(smaxage="10")
*
* NOTE: For standard formats, Symfony will also automatically choose the best
* Content-Type header for the response.
* See https://symfony.com/doc/current/quick_tour/the_controller.html#using-formats
*/
public function indexAction($page, $_format)
{
$posts = $this->getDoctrine()->getRepository(Post::class)->findLatest($page);
// Every template name also has two extensions that specify the format and
// engine for that template.
// See https://symfony.com/doc/current/templating.html#template-suffix
return $this->render('blog/index.'.$_format.'.twig', ['posts' => $posts]);
}
/**
* @Route("/posts/{slug}", name="blog_post")
* @Method("GET")
*
* 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 https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
*/
public function postShowAction(Post $post)
{
// Symfony provides a function called 'dump()' which is an improved version
// of the 'var_dump()' function. It's useful to quickly debug the contents
// of any variable, but it's not available in the 'prod' environment to
// prevent any leak of sensitive information.
// This function can be used both in PHP files and Twig templates. The only
// requirement is to have enabled the DebugBundle.
if ('dev' === $this->getParameter('kernel.environment')) {
dump($post, $this->get('security.token_storage')->getToken()->getUser(), new \DateTime());
}
return $this->render('blog/post_show.html.twig', ['post' => $post]);
}
/**
* @Route("/comment/{postSlug}/new", name="comment_new")
* @Method("POST")
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
* @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 https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
*/
public function commentNewAction(Request $request, Post $post)
{
$comment = new Comment();
$comment->setAuthor($this->getUser());
$post->addComment($comment);
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($comment);
$entityManager->flush();
// When triggering an event, you can optionally pass some information.
// For simple applications, use the GenericEvent object provided by Symfony
// to pass some PHP variables. For more complex applications, define your
// own event object classes.
// See https://symfony.com/doc/current/components/event_dispatcher/generic_event.html
$event = new GenericEvent($comment);
// When an event is dispatched, Symfony notifies it to all the listeners
// and subscribers registered to it. Listeners can modify the information
// passed in the event and they can even modify the execution flow, so
// there's no guarantee that the rest of this controller will be executed.
// See https://symfony.com/doc/current/components/event_dispatcher.html
$this->get('event_dispatcher')->dispatch(Events::COMMENT_CREATED, $event);
return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]);
}
return $this->render('blog/comment_form_error.html.twig', [
'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->createForm(CommentType::class);
return $this->render('blog/_comment_form.html.twig', [
'post' => $post,
'form' => $form->createView(),
]);
}
}