public function newAction()
{
$post = new BlogPost;
$form = $this->createBoundObjectForm($post, 'new');
if ($form->isBound() && $form->isValid()) {
$this->persist($post, true);
$this->addFlash('success');
return $this->redirectToRoute('app_blogposts_index');
}
// is not a best practice
// http://symfony.com/doc/current/best_practices/controllers.html#template-configuration
return ['form' => $form->createView()];
}
Symfony
public function newAction(Request $request)
{
$post = new BlogPost();
// you must explicitly use the request. it's good
$form = $this->createForm(NewBlogPostType::class, $post)->handleRequest($request);
// not need check request method
// http://symfony.com/doc/current/best_practices/forms.html#handling-form-submits
if ($form->isValid()) {
// if need can make shortcut method persist() in your application
$this->getDoctrine()->getManager()->persist($post);
$this->getDoctrine()->getManager()->flush();
// no magic is good
$this->addFlash('success', 'app_blogposts_new.success');
return $this->redirectToRoute('app_blogposts_index');
}
return $this->render('App:BlogPosts:new.html.twig', [
'form' => $form->createView(),
]);
}
Rad
public function deleteAction($id)
{
$blogPost = $this->findOr404('App:BlogPost', ['id' => $id]);
$this->remove($blogPost, true);
return $this->redirectToRoute('app_blogposts_index');
}
Symfony
public function deleteAction(BlogPost $blogPost)
{
// if need can make shortcut method remove() in your application
$this->getDoctrine()->getManager()->remove($blogPost);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('app_blogposts_index');
}
Rad
if (!$this->isGranted(['POST_OWNER'], $post)) {
throw $this->createAccessDeniedException();
}
Rad
Symfony
Rad
Symfony
Rad
Symfony
Rad
Symfony