Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller;
- use App\Entity\MicroPost;
- use Symfony\Component\HttpFoundation\Response;
- use App\Repository\MicroPostRepository;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
- use Symfony\Component\Routing\Annotation\Route;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\Routing\RouterInterface;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpFoundation\Request;
- /**
- * @Route("/micro-post")
- */
- class MicroPostController {
- private $microPostRepository;
- private $twig;
- private $formFactory;
- private $entityManager;
- private $router;
- private $flashBag;
- public function __construct(
- \Twig_Environment $twig, MicroPostRepository $microPostRepository, FormFactoryInterface $formFactory, EntityManagerInterface $entityManager, RouterInterface $router, FlashBagInterface $flashBag
- ) {
- $this->twig = $twig;
- $this->microPostRepository = $microPostRepository;
- $this->formFactory = $formFactory;
- $this->entityManager = $entityManager;
- $this->router = $router;
- $this->flashBag = $flashBag;
- }
- /**
- * @Route("/", name="micro_post_index")
- */
- public function index() {
- $html = $this->twig->render("micro-post/index.html.twig", [
- "posts" => $this->microPostRepository->findBy([],['time' => 'DESC'])
- ]);
- return new Response($html);
- }
- /**
- * @Route("/edit/{id}", name="micro_post_edit")
- */
- public function edit(MicroPost $microPost, Request $request) {
- $form = $this->formFactory->create(\App\Form\MicroPostType::class, $microPost);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $this->entityManager->flush();
- return new RedirectResponse($this->router->generate("micro_post_index"));
- }
- return new Response(
- $this->twig->render("micro-post/add.html.twig", [
- 'form' => $form->createView()
- ]));
- }
- /**
- * @Route("/add", name="micro_post_add")
- */
- public function add(Request $request) {
- $microPost = new MicroPost();
- $microPost->setTime(new \DateTime());
- $form = $this->formFactory->create(\App\Form\MicroPostType::class, $microPost);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $this->entityManager->persist($microPost);
- $this->entityManager->flush();
- return new RedirectResponse($this->router->generate("micro_post_index"));
- }
- return new Response(
- $this->twig->render("micro-post/add.html.twig", [
- 'form' => $form->createView()
- ]));
- }
- /**
- * @Route("/delete/{id}", name="micro_post_delete")
- */
- public function delete(MicroPost $microPost) {
- $this->entityManager->remove($microPost);
- $this->entityManager->flush();
- $this->flashBag->add('notice', 'Micro post was deleted');
- return new RedirectResponse($this->router->generate("micro_post_index"));
- }
- /**
- * @Route("/{id}", name="micro_post_single")
- */
- public function post(MicroPost $post) {
- return new Response($this->twig->render("micro-post/post.html.twig", [
- 'post' => $post
- ]
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement