Guest User

Untitled

a guest
Aug 12th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. <?php
  2.  
  3. namespace CosmeticBundle\Controller;
  4.  
  5.  
  6. use CosmeticBundle\Entity\Role;
  7. use CosmeticBundle\Entity\User;
  8. use CosmeticBundle\Form\UserType;
  9. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use CosmeticBundle\Service\Users\UserServiceInterface;
  14.  
  15. class UserController extends Controller
  16. {
  17. /**
  18. * @var UserServiceInterface
  19. */
  20. private $userService;
  21.  
  22. public function __construct(UserServiceInterface $userService)
  23. {
  24. $this->userService = $userService;
  25. }
  26.  
  27. /**
  28. * @Route("register", name="user_register",methods={"GET"})
  29. *
  30. * @param Request $request
  31. * @return Response
  32. */
  33. public function register(Request $request)
  34. {
  35.  
  36. return $this->render('users/register.html.twig',
  37. ['form' => $this->createForm(UserType::class)->createView()]
  38. );
  39. }
  40.  
  41. /**
  42. * @Route("register", methods={"POST"})
  43. *
  44. * @param Request $request
  45. * @return Response
  46. */
  47. public function registerProcess(Request $request)
  48. {
  49. $user = new User();
  50.  
  51. $form = $this->createForm(UserType::class, $user);
  52. $form->handleRequest($request);
  53. $this->userService->save($user);
  54. return $this->redirectToRoute('security_login');
  55. }
  56. /**
  57. * @Route("/dashboard", name="user_dashboard")
  58. *
  59. * @return Response|null
  60. */
  61. public function dashboard()
  62. {
  63. $userRepository = $this->getDoctrine()->getRepository(User::class);
  64. $currentUser = $userRepository->find($this->getUser());
  65. return $this->render('users/dashboard.html.twig', ['user' => $currentUser]);
  66. }
  67.  
  68. /**
  69. * @Route("/profile", name="user_profile")
  70. * @param Request $request
  71. * @param $id
  72. * @return Response|null
  73. */
  74. public function profile(Request $request)
  75. {
  76. $user = $this
  77. ->getDoctrine()
  78. ->getRepository(User::class)
  79. ->find($this->getUser());
  80. $form = $this->createForm(UserType::class, $user);
  81. $form->handleRequest($request);
  82. if ($form->isSubmitted()) {
  83. $em = $this->getDoctrine()->getManager();
  84. $em->persist($user);
  85. $em->flush();
  86. return $this->redirectToRoute('user_profile');
  87. }
  88. $userRepository = $this->getDoctrine()->getRepository(User::class);
  89. $currentUser = $userRepository->find($this->getUser());
  90. return $this->render('users/profile.html.twig',
  91. [
  92. 'user' => $currentUser,
  93. 'form' => $form->createView()
  94. ]);
  95. }
  96.  
  97. /**
  98. * @Route("logout",name="security_logout")
  99. * @throws Exception
  100. */
  101. public function logout()
  102. {
  103. throw new Exception("Logout failed");
  104. }
  105. }
  106.  
Add Comment
Please, Sign In to add comment