Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MyBlogBundle\Controller;
  4.  
  5. use MyBlogBundle\Entity\User;
  6. use MyBlogBundle\Form\UserType;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  9. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13.  
  14. class UserController extends Controller
  15. {
  16.     /**
  17.      * @Route("/register", name="user_register")
  18.      * @param Request $request
  19.      * @return \Symfony\Component\HttpFoundation\Response
  20.      */
  21.     public function registerAction(Request $request)
  22.     {
  23.         if($this->getUser() != null){
  24.             return $this->redirectToRoute("users_profile");
  25.         }
  26.         $user = new User();
  27.         $form = $this->createForm(UserType::class, $user);
  28.         $form->handleRequest($request);
  29.         if ($form->isSubmitted() && $form->isValid()) {
  30.             $password = $this->get('security.password_encoder')
  31.                 ->encodePassword($user, $user->getPassword());
  32.             $user->setPassword($password);
  33.             $user->setRoles(['ROLE_USER']);
  34.             $em = $this->getDoctrine()->getManager();
  35.             $em->persist($user);
  36.             $em->flush();
  37.             return $this->redirectToRoute("articles_all");
  38.         }
  39.         return $this->render('users/Register.html.twig', ['form' => $form->createView()]);
  40.     }
  41.  
  42.     /**
  43.      * @Route("/login", name="user_login")
  44.      * @param AuthenticationUtils $utils
  45.      * @return \Symfony\Component\HttpFoundation\Response
  46.      */
  47.     public function loginAction(AuthenticationUtils $utils)
  48.     {
  49.         if($this->getUser() != null){
  50.             return $this->redirectToRoute("users_profile");
  51.         }
  52.         return $this->render("users/Login.html.twig", ['error' => $utils->getLastAuthenticationError()]);
  53.     }
  54.  
  55.     /**
  56.      * @Route("/logout", name="user_logout")
  57.      */
  58.     public function logout()
  59.     {
  60.  
  61.     }
  62.  
  63.     /**
  64.      * @Route("/MyProfile", name="users_profile")
  65.      */
  66.     public function userProfileView()
  67.     {
  68.         if ($user = $this->getUser()){
  69.             return $this->render("users/UsersProfile.html.twig", ['user' => $user]);
  70.         }
  71.         else{
  72.             return $this->redirectToRoute("user_login");
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement