Advertisement
Guest User

Problem with Blog - PHP and Symfony exercise

a guest
Jul 23rd, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SoftUniBlogBundle\Controller;
  4.  
  5. use SoftUniBlogBundle\Entity\Article;
  6. use SoftUniBlogBundle\Form\ArticleType;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  10. use Symfony\Component\HttpFoundation\Request;
  11.  
  12. class ArticleController extends Controller
  13. {
  14.     /**
  15.      * @param Request $request
  16.      *
  17.      * @Route("/article/create", name="article_create")
  18.      * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
  19.      *
  20.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  21.      */
  22.     public function createAction(Request $request)
  23.     {
  24.         $article = new Article();
  25.         $form = $this->createForm(ArticleType::class, $article);
  26.  
  27.         return $this->render('article/create.html.twig',
  28.             array('form' => $form->createView()));
  29.     }
  30.  
  31.     public function create(Request $request)
  32.     {
  33.         $article = new Article();
  34.         $form = $this->createForm(ArticleType::class, $article);
  35.  
  36.         $form->handleRequest($request);
  37.  
  38.         if ($form->isValid())
  39.         {
  40.             $article->setAuthor($this->getUser());
  41.             $em = $this->getDoctrine()->getManager();
  42.             $em->persist($article);
  43.             $em->flush();
  44.  
  45.             return $this->redirectToRoute('blog_index');
  46.         }
  47.         return $this->render('article/create.html.twig',
  48.             array('form' => $form->createView()));
  49.     }
  50.  
  51.     /**
  52.      * @Route("/article/{id}", name="article_view")
  53.      * @param $id
  54.      * @return \Symfony\Component\HttpFoundation\Response
  55.      */
  56.     public function viewArticle($id)
  57.     {
  58.         $article = $this->getDoctrine()->getRepository(Article::class)->find($id);
  59.  
  60.         return $this->render('article/create.html.twig', ['article' => $article]);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement