Ejemplo n.º 1
0
 public function addPostAction(Request $request)
 {
     $post = new Post();
     $post->setDate(new \DateTime('now'));
     $post->setAuthor($this->getUser());
     $form = $this->createForm(PostType::class, $post);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $file = $post->getImage();
         if ($file) {
             $fileName = md5(uniqid()) . '.' . $file->guessExtension();
             $imagesDir = $this->container->getParameter('kernel.root_dir') . '/../web/public/post-images';
             $file->move($imagesDir, $fileName);
             $post->setImage($fileName);
         } else {
             $post->setImage('default.jpg');
         }
         $em = $this->getDoctrine()->getManager();
         $em->persist($post);
         $em->flush();
         $this->addFlash('notice', 'Twój post został dodany!');
         return $this->redirectToRoute('blog_showpost', array('id' => $post->getId()));
     } else {
         return $this->render('BlogBundle:Dashboard:addPost.html.twig', array('post' => $post, 'form' => $form->createView()));
     }
 }