Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use App\Entity\MicroPost;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use App\Repository\MicroPostRepository;
  8. use Symfony\Component\Form\FormFactoryInterface;
  9. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15.  
  16. /**
  17.  * @Route("/micro-post")
  18.  */
  19. class MicroPostController {
  20.  
  21.     private $microPostRepository;
  22.     private $twig;
  23.     private $formFactory;
  24.     private $entityManager;
  25.     private $router;
  26.     private $flashBag;
  27.  
  28.     public function __construct(
  29.     \Twig_Environment $twig, MicroPostRepository $microPostRepository, FormFactoryInterface $formFactory, EntityManagerInterface $entityManager, RouterInterface $router, FlashBagInterface $flashBag
  30.     ) {
  31.         $this->twig = $twig;
  32.         $this->microPostRepository = $microPostRepository;
  33.         $this->formFactory = $formFactory;
  34.         $this->entityManager = $entityManager;
  35.         $this->router = $router;
  36.         $this->flashBag = $flashBag;
  37.     }
  38.  
  39.     /**
  40.      * @Route("/", name="micro_post_index")
  41.      */
  42.     public function index() {
  43.         $html = $this->twig->render("micro-post/index.html.twig", [
  44.             "posts" => $this->microPostRepository->findBy([],['time' => 'DESC'])
  45.         ]);
  46.  
  47.         return new Response($html);
  48.     }
  49.    
  50.     /**
  51.      * @Route("/edit/{id}", name="micro_post_edit")
  52.      */
  53.     public function edit(MicroPost $microPost, Request $request) {
  54.         $form = $this->formFactory->create(\App\Form\MicroPostType::class, $microPost);
  55.         $form->handleRequest($request);
  56.  
  57.         if ($form->isSubmitted() && $form->isValid()) {
  58.             $this->entityManager->flush();
  59.  
  60.             return new RedirectResponse($this->router->generate("micro_post_index"));
  61.         }
  62.  
  63.         return new Response(
  64.                 $this->twig->render("micro-post/add.html.twig", [
  65.                     'form' => $form->createView()
  66.         ]));
  67.     }
  68.  
  69.     /**
  70.      * @Route("/add", name="micro_post_add")
  71.      */
  72.     public function add(Request $request) {
  73.  
  74.         $microPost = new MicroPost();
  75.         $microPost->setTime(new \DateTime());
  76.  
  77.         $form = $this->formFactory->create(\App\Form\MicroPostType::class, $microPost);
  78.         $form->handleRequest($request);
  79.  
  80.         if ($form->isSubmitted() && $form->isValid()) {
  81.             $this->entityManager->persist($microPost);
  82.             $this->entityManager->flush();
  83.  
  84.             return new RedirectResponse($this->router->generate("micro_post_index"));
  85.         }
  86.  
  87.         return new Response(
  88.                 $this->twig->render("micro-post/add.html.twig", [
  89.                     'form' => $form->createView()
  90.         ]));
  91.     }
  92.  
  93.     /**
  94.      * @Route("/delete/{id}", name="micro_post_delete")
  95.      */
  96.     public function delete(MicroPost $microPost) {
  97.         $this->entityManager->remove($microPost);
  98.         $this->entityManager->flush();
  99.  
  100.         $this->flashBag->add('notice', 'Micro post was deleted');
  101.  
  102.         return new RedirectResponse($this->router->generate("micro_post_index"));
  103.     }
  104.  
  105.  
  106.     /**
  107.      * @Route("/{id}", name="micro_post_single")
  108.      */
  109.     public function post(MicroPost $post) {
  110.         return new Response($this->twig->render("micro-post/post.html.twig", [
  111.                     'post' => $post
  112.                         ]
  113.         ));
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement