Advertisement
sanjiisan

Untitled

Aug 6th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5. use AppBundle\Entity\Tweet;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10.  
  11. class TweetController extends Controller
  12. {
  13. /**
  14. * @Route("/create")
  15. */
  16. public function createAction()
  17. {
  18. $form = $this->generateForm(new Tweet());
  19.  
  20. return $this->render('AppBundle:Tweet:create.html.twig', ['form' => $form->createView()]);
  21. }
  22.  
  23. /**
  24. * @Route("/new", name="new_tweet")
  25. */
  26. public function newAction(Request $request)
  27. {
  28. $tweet = new Tweet();
  29. $form = $this->generateForm($tweet);
  30.  
  31. $form->handleRequest($request);
  32. if ($form->isSubmitted()) {
  33. $tweet = $form->getData();
  34.  
  35. $em = $this->getDoctrine()->getEntityManager();
  36. $em->persist($tweet);
  37. $em->flush();
  38. }
  39.  
  40. return new Response('Zadziałało');
  41. }
  42.  
  43. /**
  44. * @Route("/update/{id}/")
  45. */
  46. public function updateAction(Request $request, $id)
  47. {
  48. $tweet = $this->getDoctrine()->getEntityManager()->getRepository('AppBundle:Tweet')->find($id);
  49.  
  50. $form = $this->createFormBuilder($tweet)
  51. ->add('name', 'text')
  52. ->add('content', 'text')
  53. ->add('submit', 'submit')
  54. ->getForm();
  55.  
  56. $form->handleRequest($request);
  57. if ($form->isSubmitted()) {
  58. $tweet = $form->getData();
  59.  
  60. $em = $this->getDoctrine()->getEntityManager();
  61. $em->persist($tweet);
  62. $em->flush();
  63.  
  64. return new Response('Updejt');
  65. }
  66.  
  67. return $this->render('@App/Tweet/create.html.twig', ['form' => $form->createView()]);
  68. }
  69.  
  70.  
  71. private function generateForm($tweet)
  72. {
  73. return $this->createFormBuilder($tweet)
  74. ->setAction(
  75. $this->generateUrl('new_tweet')
  76. )
  77. ->add('name', 'text')
  78. ->add('content', 'text')
  79. ->add('submit', 'submit')
  80. ->getForm();
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement