Advertisement
Guest User

Untitled

a guest
Jul 26th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SoftUniBlogBundle\Controller;
  4.  
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use SoftUniBlogBundle\Entity\Article;
  9. use SoftUniBlogBundle\Entity\User;
  10. use SoftUniBlogBundle\Form\UserType;
  11. use SoftUniBlogBundle\Repository\UserRepository;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpFoundation\Request;
  14.  
  15. class UserController extends Controller
  16. {
  17. /**
  18. * @Route("/register", name="user_register")
  19. * @param Request $request
  20. * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  21. */
  22. public function registerAction(Request $request)
  23. {
  24. // 1) build the form
  25. $user = new User();
  26. $form = $this->createForm(UserType::class, $user);
  27.  
  28. // 2) handle the submit (will only happen on POST)
  29. $form->handleRequest($request);
  30.  
  31. if ($form->isSubmitted() && $form->isValid()) {
  32.  
  33. // 3) Encode the password (you could also do this via Doctrine listener)
  34. $password = $this->get('security.password_encoder')
  35. ->encodePassword($user, $user->getPassword());
  36. $user->setPassword($password);
  37.  
  38. // 4) save the User!
  39. $em = $this->getDoctrine()->getManager();
  40. $em->persist($user);
  41. $em->flush();
  42.  
  43. // ... do any other work - like sending them an email, etc
  44. // maybe set a "flash" success message for the user
  45.  
  46. return $this->redirectToRoute('security_login');
  47. }
  48.  
  49. return $this->render(
  50. 'user/register.html.twig',
  51. array('form' => $form->createView())
  52. );
  53. }
  54.  
  55.  
  56.  
  57. /**
  58. * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
  59. * @Route("/profile", name="user_profile")
  60. */
  61. public function profileAction()
  62. {
  63. $user = $this->getUser();
  64. return $this->render("user/profile.html.twig", ['user'=>$user]);
  65. }
  66.  
  67. /**
  68. * @Route("/user/articles", name="user_articles")
  69. * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
  70. * @Method("GET")
  71. */
  72. public function userArticles()
  73. {
  74. $user = $this->getUser();
  75.  
  76. $articles = $this->getDoctrine()->getRepository(Article::class)
  77. ->findBy(['authorId' => $user]);
  78.  
  79. return $this->render('user/articles.html.twig', ['articles' => $articles]);
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement