KnpLabs / KnpRadBundle

Rapid Application Development for Symfony2 [UNMAINTAINED]
MIT License
291 stars 49 forks source link

Bundle is outdated #216

Open peter-gribanov opened 8 years ago

peter-gribanov commented 8 years ago

Rad

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();
}

Symfony

$this->denyAccessUnlessGranted('POST_OWNER', $post);

Rad

public function showAction(BlogPost $blogPost)
{
    if (!$this->isGranted(['POST_SHOW'], $blogPost)) {
        throw $this->createAccessDeniedException();
    }

    return ['blogPost' => $blogPost];
}

Symfony

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

/**
 * @Security("is_granted('POST_SHOW', blogPost)")
 */
public function showAction(BlogPost $blogPost)
{
    return $this->render('App:BlogPosts:show.html.twig', [
        'blog_post' => $blogPost,
    ]);
}