Advertisement
Guest User

Error with Blog PHP & Symfony

a guest
Jul 24th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 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.      * Route("/article/create", name="article_create")
  16.      * @return \Symfony\Component\HttpFoundation\Response
  17.      */
  18.     public function create(Request $request)
  19.     {
  20.         $article = new Article();
  21.         $form = $this->createForm(ArticleType::class, $article);
  22.         $form->handleRequest($request);
  23.  
  24.         if ($form->isValid())
  25.         {
  26.             $article->setAuthor($this->getUser());
  27.             $em = $this->getDoctrine()->getManager();
  28.             $em->persist($article);
  29.             $em->flush();
  30.  
  31.             return $this->redirectToRoute('blog_index');
  32.         }
  33.         return $this->render('article/create.html.twig',
  34.             [
  35.                 'form' => $form->createView()
  36.             ]
  37.         );
  38.     }
  39.  
  40.     /**
  41.      * @Route("/article/{id}", name="article_view")
  42.      * @param $id
  43.      * @return \Symfony\Component\HttpFoundation\Response
  44.      */
  45.     public function viewArticle($id)
  46.     {
  47.         $article = $this->getDoctrine()->getRepository(Article::class)->find($id);
  48.  
  49.         return $this->render('article/create.html.twig', ['article' => $article]);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement