Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: cezary
  5.  * Date: 27.10.16
  6.  * Time: 22:13
  7.  */
  8. // src/AppBundle/Controller/RegistrationController.php
  9. namespace AppBundle\Controller;
  10.  
  11. use AppBundle\Form\UserType;
  12. use AppBundle\Entity\User;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  14. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  15. use Symfony\Component\HttpFoundation\Request;
  16.  
  17. class RegistrationController extends Controller
  18. {
  19.     /**
  20.      * @Route("/register", name="user_registration")
  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.         if ($form->isSubmitted() && $form->isValid()) {
  31.  
  32.             // 3) Encode the password (you could also do this via Doctrine listener)
  33.             $password = $this->get('security.password_encoder')
  34.                 ->encodePassword($user, $user->getPlainPassword());
  35.             $user->setPassword($password);
  36.  
  37.             // 4) save the User!
  38.             $em = $this->getDoctrine()->getManager();
  39.             $em->persist($user);
  40.             $em->flush();
  41.  
  42.             // ... do any other work - like sending them an email, etc
  43.             // maybe set a "flash" success message for the user
  44.  
  45.             return $this->redirectToRoute('security/login.html.twig');
  46.         }
  47.  
  48.         return $this->render(
  49.             'registration/register.html.twig',
  50.             array('form' => $form->createView())
  51.         );
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement