<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Doctrine\Persistence\ManagerRegistry;
class RegistrationController extends AbstractController
{
private $passwordHasher;
public function __construct(UserPasswordHasherInterface $passwordHasher)
{
$this->passwordHasher = $passwordHasher;
}
/**
* @Route("/registration", name="registration")
*/
public function index(Request $request, ManagerRegistry $doctrine)
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$user->getPassword()
);
$user->setPassword($hashedPassword);
// Set their role
$user->setRoles(['ROLE_USER']);
$em = $doctrine->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('app_login');
}
return $this->render('registration/index.html.twig', [
'form' => $form->createView(),
]);
}
}