Advertisement
StoyanGrigorov

Code snipet

Nov 3rd, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1.     /**
  2.      * @Route("/article/delete/{id}", name="article_delete")
  3.      * @param $id
  4.      * @param Request $request
  5.      * @return \Symfony\Component\HttpFoundation\Response
  6.      */
  7.     public function delete($id, Request $request)
  8.     {
  9.         $article = $this->getDoctrine()->getRepository(Article::class)->find($id);
  10.  
  11.         if($article === null){
  12.             return $this->redirectToRoute("blog_index");
  13.         }
  14.  
  15.         $currentUser = $this->getUser();
  16.  
  17.         if(!$currentUser->isAuthor($article) && !$currentUser->isAdmin())
  18.         {
  19.             return $this->redirectToRoute("blog_index");
  20.         }
  21.  
  22.         $form = $this->createForm(ArticleType::class, $article);
  23.  
  24.         $form->handleRequest($request);
  25.  
  26.         if($form->isSubmitted() && $form->isValid())
  27.         {
  28.             $em = $this->getDoctrine()->getManager();
  29.             $em->remove($article);
  30.             $em->flush();
  31.  
  32.             return $this->redirectToRoute('blog_index');
  33.         }
  34.  
  35.         return $this->render('article/delete.html.twig', array('article' => $article, 'form' => $form->createView()));
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement