Skip to content

Commit 8d0cc81

Browse files
committed
Added @method constraints where appropriate and added more help notes
1 parent 27a175e commit 8d0cc81

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

src/AppBundle/Controller/Admin/BlogController.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public function indexAction()
5656
* Creates a new Post entity.
5757
*
5858
* @Route("/new", name="admin_post_new")
59+
* @Method({"GET", "POST"})
60+
*
61+
* NOTE: the Method annotation is optional, but it's a recommended practice
62+
* to constraint the HTTP methods each controller responds to (by default
63+
* it responds to all methods).
5964
*/
6065
public function newAction(Request $request)
6166
{
@@ -84,12 +89,12 @@ public function newAction(Request $request)
8489
/**
8590
* Finds and displays a Post entity.
8691
*
87-
* @Route("/{id}", name="admin_post_show")
92+
* @Route("/{id}", requirements={"id" = "\d+"}, name="admin_post_show")
8893
* @Method("GET")
8994
* @Security("post.isAuthor(user)")
9095
*
91-
* NOTE: You can also centralize security logic by using a "voter"
92-
* http://symfony.com/doc/current/cookbook/security/voters_data_permission.html
96+
* NOTE: You can also centralize security logic by using a "voter"
97+
* See http://symfony.com/doc/current/cookbook/security/voters_data_permission.html
9398
*/
9499
public function showAction(Post $post)
95100
{
@@ -104,7 +109,8 @@ public function showAction(Post $post)
104109
/**
105110
* Displays a form to edit an existing Post entity.
106111
*
107-
* @Route("/{id}/edit", name="admin_post_edit")
112+
* @Route("/{id}/edit", requirements={"id" = "\d+"}, name="admin_post_edit")
113+
* @Method({"GET", "POST"})
108114
* @Security("post.isAuthor(user)")
109115
*/
110116
public function editAction(Post $post, Request $request)
@@ -136,6 +142,10 @@ public function editAction(Post $post, Request $request)
136142
* @Route("/{id}", name="admin_post_delete")
137143
* @Method("DELETE")
138144
* @Security("post.isAuthor(user)")
145+
*
146+
* The Security annotation value is an expression (if it evaluates to false,
147+
* the authorization mechanism will prevent the user accessing this resource).
148+
* The isAuthor() method is defined in the AppBundle\Entity\Post entity.
139149
*/
140150
public function deleteAction(Request $request, Post $post)
141151
{
@@ -155,6 +165,12 @@ public function deleteAction(Request $request, Post $post)
155165
/**
156166
* Creates a form to delete a Post entity by id.
157167
*
168+
* This is necessary because browsers don't support HTTP methods different
169+
* from GET and POST. Since the controller that removes the blog posts expects
170+
* a DELETE method, the trick is to create a simple form that *fakes* the
171+
* HTTP DELETE method.
172+
* See http://symfony.com/doc/current/cookbook/routing/method_parameters.html.
173+
*
158174
* @param Post $post The post object
159175
*
160176
* @return \Symfony\Component\Form\Form The form

src/AppBundle/Controller/BlogController.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public function indexAction()
4545

4646
/**
4747
* @Route("/posts/{slug}", name="blog_post")
48+
*
49+
* NOTE: The $post controller argument is automatically injected by Symfony
50+
* after performing a database query looking for a Post with the 'slug'
51+
* value given in the route.
52+
* See http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
4853
*/
4954
public function postShowAction(Post $post)
5055
{
@@ -54,12 +59,13 @@ public function postShowAction(Post $post)
5459
/**
5560
* @Route("/comment/{postSlug}/new", name = "comment_new")
5661
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
57-
* @Method("POST")
58-
*
59-
* NOTE: The following ParamConverter mapping is required because the route parameter
60-
* (postSlug) doesn't match any of the Doctrine entity properties (slug):
6162
*
63+
* @Method("POST")
6264
* @ParamConverter("post", options={"mapping": {"postSlug": "slug"}})
65+
*
66+
* NOTE: The ParamConverter mapping is required because the route parameter
67+
* (postSlug) doesn't match any of the Doctrine entity properties (slug).
68+
* See http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
6369
*/
6470
public function commentNewAction(Request $request, Post $post)
6571
{
@@ -88,10 +94,12 @@ public function commentNewAction(Request $request, Post $post)
8894
}
8995

9096
/**
91-
* Called via the render() function in Twig.
97+
* This controller is called directly via the render() function in the
98+
* blog/post_show.html.twig template. That's why it's not needed to define
99+
* a route name for it.
92100
*
93101
* The "id" of the Post is passed in and then turned into a Post object
94-
* by the ParamConverter.
102+
* automatically by the ParamConverter.
95103
*
96104
* @param Post $post
97105
*
@@ -107,6 +115,11 @@ public function commentFormAction(Post $post)
107115
));
108116
}
109117

118+
/**
119+
* This is a utility method used to create comment forms. It's recommended
120+
* to not define this kind of methods in a controller class, but sometimes
121+
* is convenient for defining small methods.
122+
*/
110123
private function createCommentForm()
111124
{
112125
$form = $this->createForm(new CommentType());

src/AppBundle/Controller/SecurityController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* Controller used to manage the application security.
20-
* See http://symfony.com/doc/current/cookbook/security/form_login_setup.html
20+
* See http://symfony.com/doc/current/cookbook/security/form_login_setup.html.
2121
*
2222
* @author Ryan Weaver <[email protected]>
2323
* @author Javier Eguiluz <[email protected]>

0 commit comments

Comments
 (0)