Skip to content

Commit c700a55

Browse files
committed
feature #427 Do not use a Symfony Form to delete an article (lyrixx)
This PR was merged into the master branch. Discussion ---------- Do not use a Symfony Form to delete an article Because it's simpler without a SF Form Commits ------- 9f8a40b Do not use a Symfony Form to delete an article
2 parents d121e39 + 9f8a40b commit c700a55

File tree

4 files changed

+21
-54
lines changed

4 files changed

+21
-54
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{ include('blog/_delete_post_confirmation.html.twig') }}
2+
<form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post" data-confirmation="true">
3+
<input type="hidden" name="token" value="{{ csrf_token('delete') }}" />
4+
<input type="submit" value="{{ 'action.delete_post'|trans }}" class="btn btn-lg btn-block btn-danger" />
5+
</form>

app/Resources/views/admin/blog/edit.html.twig

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66
<h1>{{ 'title.edit_post'|trans({'%id%': post.id}) }}</h1>
77

88
{{ include('admin/blog/_form.html.twig', {
9-
form: edit_form,
9+
form: form,
1010
button_label: 'action.save'|trans,
1111
include_back_to_home_link: true,
1212
}, with_context = false) }}
1313
{% endblock %}
1414

1515
{% block sidebar %}
1616
<div class="section actions">
17-
{{ include('admin/blog/_form.html.twig', {
18-
form: delete_form,
19-
button_label: 'action.delete_post'|trans,
20-
button_css: 'btn btn-lg btn-block btn-danger',
21-
show_confirmation: true,
22-
}, with_context = false) }}
17+
{{ include('admin/blog/_delete_form.html.twig', { post: post }, with_context = false) }}
2318
</div>
2419

2520
{{ parent() }}

app/Resources/views/admin/blog/show.html.twig

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@
3838
</div>
3939

4040
<div class="section">
41-
{{ include('admin/blog/_form.html.twig', {
42-
form: delete_form,
43-
button_label: 'action.delete_post'|trans,
44-
button_css: 'btn btn-lg btn-block btn-danger',
45-
show_confirmation: true,
46-
}, with_context = false) }}
41+
{{ include('admin/blog/_delete_form.html.twig', { post: post }, with_context = false) }}
4742
</div>
4843

4944
{{ parent() }}

src/AppBundle/Controller/Admin/BlogController.php

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,8 @@ public function showAction(Post $post)
126126
throw $this->createAccessDeniedException('Posts can only be shown to their authors.');
127127
}
128128

129-
$deleteForm = $this->createDeleteForm($post);
130-
131129
return $this->render('admin/blog/show.html.twig', [
132130
'post' => $post,
133-
'delete_form' => $deleteForm->createView(),
134131
]);
135132
}
136133

@@ -148,12 +145,11 @@ public function editAction(Post $post, Request $request)
148145

149146
$entityManager = $this->getDoctrine()->getManager();
150147

151-
$editForm = $this->createForm(PostType::class, $post);
152-
$deleteForm = $this->createDeleteForm($post);
148+
$form = $this->createForm(PostType::class, $post);
153149

154-
$editForm->handleRequest($request);
150+
$form->handleRequest($request);
155151

156-
if ($editForm->isSubmitted() && $editForm->isValid()) {
152+
if ($form->isSubmitted() && $form->isValid()) {
157153
$post->setSlug($this->get('slugger')->slugify($post->getTitle()));
158154
$entityManager->flush();
159155

@@ -164,16 +160,15 @@ public function editAction(Post $post, Request $request)
164160

165161
return $this->render('admin/blog/edit.html.twig', [
166162
'post' => $post,
167-
'edit_form' => $editForm->createView(),
168-
'delete_form' => $deleteForm->createView(),
163+
'form' => $form->createView(),
169164
]);
170165
}
171166

172167
/**
173168
* Deletes a Post entity.
174169
*
175-
* @Route("/{id}", name="admin_post_delete")
176-
* @Method("DELETE")
170+
* @Route("/{id}/delete", name="admin_post_delete")
171+
* @Method("POST")
177172
* @Security("post.isAuthor(user)")
178173
*
179174
* The Security annotation value is an expression (if it evaluates to false,
@@ -182,40 +177,17 @@ public function editAction(Post $post, Request $request)
182177
*/
183178
public function deleteAction(Request $request, Post $post)
184179
{
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+
}
187183

188-
if ($form->isSubmitted() && $form->isValid()) {
189-
$entityManager = $this->getDoctrine()->getManager();
184+
$entityManager = $this->getDoctrine()->getManager();
190185

191-
$entityManager->remove($post);
192-
$entityManager->flush();
186+
$entityManager->remove($post);
187+
$entityManager->flush();
193188

194-
$this->addFlash('success', 'post.deleted_successfully');
195-
}
189+
$this->addFlash('success', 'post.deleted_successfully');
196190

197191
return $this->redirectToRoute('admin_post_index');
198192
}
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-
}
221193
}

0 commit comments

Comments
 (0)