@@ -126,11 +126,8 @@ public function showAction(Post $post)
126
126
throw $ this ->createAccessDeniedException ('Posts can only be shown to their authors. ' );
127
127
}
128
128
129
- $ deleteForm = $ this ->createDeleteForm ($ post );
130
-
131
129
return $ this ->render ('admin/blog/show.html.twig ' , [
132
130
'post ' => $ post ,
133
- 'delete_form ' => $ deleteForm ->createView (),
134
131
]);
135
132
}
136
133
@@ -148,12 +145,11 @@ public function editAction(Post $post, Request $request)
148
145
149
146
$ entityManager = $ this ->getDoctrine ()->getManager ();
150
147
151
- $ editForm = $ this ->createForm (PostType::class, $ post );
152
- $ deleteForm = $ this ->createDeleteForm ($ post );
148
+ $ form = $ this ->createForm (PostType::class, $ post );
153
149
154
- $ editForm ->handleRequest ($ request );
150
+ $ form ->handleRequest ($ request );
155
151
156
- if ($ editForm ->isSubmitted () && $ editForm ->isValid ()) {
152
+ if ($ form ->isSubmitted () && $ form ->isValid ()) {
157
153
$ post ->setSlug ($ this ->get ('slugger ' )->slugify ($ post ->getTitle ()));
158
154
$ entityManager ->flush ();
159
155
@@ -164,16 +160,15 @@ public function editAction(Post $post, Request $request)
164
160
165
161
return $ this ->render ('admin/blog/edit.html.twig ' , [
166
162
'post ' => $ post ,
167
- 'edit_form ' => $ editForm ->createView (),
168
- 'delete_form ' => $ deleteForm ->createView (),
163
+ 'form ' => $ form ->createView (),
169
164
]);
170
165
}
171
166
172
167
/**
173
168
* Deletes a Post entity.
174
169
*
175
- * @Route("/{id}", name="admin_post_delete")
176
- * @Method("DELETE ")
170
+ * @Route("/{id}/delete ", name="admin_post_delete")
171
+ * @Method("POST ")
177
172
* @Security("post.isAuthor(user)")
178
173
*
179
174
* The Security annotation value is an expression (if it evaluates to false,
@@ -182,40 +177,17 @@ public function editAction(Post $post, Request $request)
182
177
*/
183
178
public function deleteAction (Request $ request , Post $ post )
184
179
{
185
- $ form = $ this ->createDeleteForm ($ post );
186
- $ form ->handleRequest ($ request );
180
+ if (!$ this ->isCsrfTokenValid ('delete ' , $ request ->request ->get ('token ' ))) {
181
+ return $ this ->redirectToRoute ('admin_post_index ' );
182
+ }
187
183
188
- if ($ form ->isSubmitted () && $ form ->isValid ()) {
189
- $ entityManager = $ this ->getDoctrine ()->getManager ();
184
+ $ entityManager = $ this ->getDoctrine ()->getManager ();
190
185
191
- $ entityManager ->remove ($ post );
192
- $ entityManager ->flush ();
186
+ $ entityManager ->remove ($ post );
187
+ $ entityManager ->flush ();
193
188
194
- $ this ->addFlash ('success ' , 'post.deleted_successfully ' );
195
- }
189
+ $ this ->addFlash ('success ' , 'post.deleted_successfully ' );
196
190
197
191
return $ this ->redirectToRoute ('admin_post_index ' );
198
192
}
199
-
200
- /**
201
- * Creates a form to delete a Post entity by id.
202
- *
203
- * This is necessary because browsers don't support HTTP methods different
204
- * from GET and POST. Since the controller that removes the blog posts expects
205
- * a DELETE method, the trick is to create a simple form that *fakes* the
206
- * HTTP DELETE method.
207
- * See http://symfony.com/doc/current/cookbook/routing/method_parameters.html.
208
- *
209
- * @param Post $post The post object
210
- *
211
- * @return \Symfony\Component\Form\Form The form
212
- */
213
- private function createDeleteForm (Post $ post )
214
- {
215
- return $ this ->createFormBuilder ()
216
- ->setAction ($ this ->generateUrl ('admin_post_delete ' , ['id ' => $ post ->getId ()]))
217
- ->setMethod ('DELETE ' )
218
- ->getForm ()
219
- ;
220
- }
221
193
}
0 commit comments