ambiorixdr

ArticleController

Mar 15th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SoftUniBlogBundle\Controller;
  4.  
  5. use SoftUniBlogBundle\Entity\Article;
  6. use SoftUniBlogBundle\Form\ArticleType;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  10. use Symfony\Component\HttpFoundation\Request;
  11.  
  12. class ArticleController extends Controller
  13. {
  14.     /**
  15.      * @param Request $request
  16.      *
  17.      * @Route("/article/create", name="article_create")
  18.      * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
  19.      *
  20.      * @return \Symfony\Component\HttpFoundation\Response
  21.      *
  22.      */
  23.     public function create(Request $request)
  24.     {
  25.         $article = new Article();
  26.         $form = $this->createForm(ArticleType::class, $article);
  27.  
  28.         $form->handleRequest($request);
  29.  
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.  
  32.             $article->setAuthor($this->getUser());
  33.             $em = $this->getDoctrine()->getManager();
  34.             $em->persist($article);
  35.             $em->flush();
  36.  
  37.             return $this->redirectToRoute('blog_index');
  38.         }
  39.  
  40.         return $this->render('article/create.html.twig',
  41.             array('form' => $form->createView()));
  42.     }
  43.  
  44.     /**
  45.      * @Route("/article/{id}", name="article_view")
  46.      * @param $id
  47.      * @return \Symfony\Component\HttpFoundation\Response
  48.      */
  49.     public function viewArticle($id)
  50.     {
  51.         $article = $this->getDoctrine()->getRepository(Article::class)->find($id);
  52.  
  53.         return $this->render('article/article.html.twig', ['article' => $article]);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment