<?php
// src/Controller/RegistrationController.php
namespace App\Controller;
use App\Entity\AppUser;
use App\Form\RegistrationFormType;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
/**
* @Route("/{_locale}/register")
*/
class RegistrationController extends AbstractController
{
/**
* @Route("/", name="registration_register")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function register(Request $request, UserPasswordHasherInterface $passwordHasher)
{
// ... e.g. get the user data from a registration form
$user = new AppUser;
$plaintextPassword = '';
$em = $this->getDoctrine()
->getManager();
$form = $this->createForm(RegistrationFormType::class, $user);
if($request->getMethod() == 'POST'){
$form->handleRequest($request);
if($form->isValid()){
$data = $form->getData();
$plaintextPassword = '';
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword($user, $data->getPlainPassword());
$user->setPassword($hashedPassword);
$em->persist($user);
$em->flush();
}
}
return $this->render('registration/register.html.twig', [
'form' => $form->createView()
]);
}
}