luoni

Article

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