Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SoftUniBlogBundle\Controller;
  4.  
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use SoftUniBlogBundle\Entity\Article;
  9. use SoftUniBlogBundle\Entity\User;
  10. use SoftUniBlogBundle\Form\ArticleType;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\HttpFoundation\Request;
  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. public function createAction(Request $request)
  25. {
  26. $article = new Article();
  27. $form = $this->createForm(ArticleType::class, $article);
  28. $form->handleRequest($request);
  29.  
  30. if ($form->isSubmitted() && $form->isValid()) {
  31. $article->setAuthor($this->getUser());
  32. $entityManager = $this->getDoctrine()->getManager();
  33. $entityManager->persist($article);
  34. $entityManager->flush();
  35.  
  36. return $this->redirectToRoute("blog_index");
  37. }
  38. return $this->render("article/create.html.twig",
  39. [
  40. "form"=>$form->createView()
  41. ]);
  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. return $this->render("article/article.html.twig",
  53. [
  54. "article" => $article
  55. ]);
  56. }
  57.  
  58. /**
  59. * @Route("/article/allarticles", name="123")
  60. * @return \Symfony\Component\HttpFoundation\Response
  61. */
  62. public function test()
  63. {
  64. return $this->render("article/allarticles.html.twig");
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement