Guest User

Untitled

a guest
Apr 27th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. <?php
  2. namespace AppBundle\Controller;
  3.  
  4. use AppBundle\Form\UserType;
  5. use AppBundle\Entity\User;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpFoundation\Request;
  9.  
  10. class RegistrationController extends Controller
  11. {
  12.     /**
  13.      * @Route("/register", name="user_registration")
  14.      */
  15.     public function registerAction(Request $request)
  16.     {
  17.         // 1) build the form
  18.         $user = new User();
  19.         $form = $this->createForm(UserType::class, $user);
  20.  
  21.         // 2) handle the submit (will only happen on POST)
  22.         $form->handleRequest($request);
  23.         if ($form->isSubmitted() && $form->isValid()) {
  24.  
  25.             // 3) Encode the password (you could also do this via Doctrine listener)
  26.             $password = $this->get('security.password_encoder')
  27.                 ->encodePassword($user, $user->getPlainPassword());
  28.             $user->setPassword($password);
  29.  
  30.             // 4) save the User!
  31.             $em = $this->getDoctrine()->getManager();
  32.             $em->persist($user);
  33.             $em->flush();
  34.  
  35.             // ... do any other work - like sending them an email, etc
  36.             // maybe set a "flash" success message for the user
  37.  
  38.             return $this->redirectToRoute('replace_with_some_route');
  39.         }
  40.  
  41.         return $this->render(
  42.             'registration/register.html.twig',
  43.             array('form' => $form->createView())
  44.         );
  45.     }
  46. }
Add Comment
Please, Sign In to add comment