Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundle\Controller;
- use AppBundle\Form\UserType;
- use AppBundle\Entity\User;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- class RegistrationController extends Controller
- {
- /**
- * @Route("/register", name="user_registration")
- */
- public function registerAction(Request $request)
- {
- // 1) build the form
- $user = new User();
- $form = $this->createForm(UserType::class, $user);
- // 2) handle the submit (will only happen on POST)
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- // 3) Encode the password (you could also do this via Doctrine listener)
- $password = $this->get('security.password_encoder')
- ->encodePassword($user, $user->getPlainPassword());
- $user->setPassword($password);
- // 4) save the User!
- $em = $this->getDoctrine()->getManager();
- $em->persist($user);
- $em->flush();
- // ... do any other work - like sending them an email, etc
- // maybe set a "flash" success message for the user
- return $this->redirectToRoute('replace_with_some_route');
- }
- return $this->render(
- 'registration/register.html.twig',
- array('form' => $form->createView())
- );
- }
- }
Add Comment
Please, Sign In to add comment