Created
August 20, 2015 13:54
-
-
Save benbieler/bc576cab7dccdee24412 to your computer and use it in GitHub Desktop.
A simple symfony registration script. Entity - Form - Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraint as Assert; | |
/** | |
* User | |
* | |
* @ORM\Table() | |
* @ORM\Entity | |
*/ | |
class User | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* @Assert\NotBlank() | |
* @ORM\Column(name="name", type="string", length=255) | |
*/ | |
private $name; | |
/** | |
* @var string | |
* @Assert\NotBlank() | |
* @Assert\Email() | |
* @ORM\Column(name="email", type="string", length=255) | |
*/ | |
private $email; | |
/** | |
* @var string | |
* @Assert\NotBlank() | |
* @ORM\Column(name="password", type="string", length=255) | |
*/ | |
private $password; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set name | |
* | |
* @param string $name | |
* @return User | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
/** | |
* Get name | |
* | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* Set email | |
* | |
* @param string $email | |
* @return User | |
*/ | |
public function setEmail($email) | |
{ | |
$this->email = $email; | |
return $this; | |
} | |
/** | |
* Get email | |
* | |
* @return string | |
*/ | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
/** | |
* Set password | |
* | |
* @param string $password | |
* @return User | |
*/ | |
public function setPassword($password) | |
{ | |
$this->password = password_hash($password, PASSWORD_BCRYPT); | |
return $this; | |
} | |
/** | |
* Get password | |
* | |
* @return string | |
*/ | |
public function getPassword() | |
{ | |
return $this->password; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use AppBundle\Entity\User; | |
use AppBundle\Form\UserLogin; | |
use AppBundle\Form\UserRegister; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
class UserController extends Controller | |
{ | |
/** | |
* Registers a new user to the Mysql DB | |
* | |
* @Route("/register", name="register_user") | |
* @Template() | |
* @return array | |
*/ | |
public function indexAction() | |
{ | |
$model = new User(); | |
$form = $this->createForm(new UserRegister(), $model); | |
if($form->isValid() && $form->isSubmitted()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($model); | |
$em->flush(); | |
return $this->redirect($this->generateUrl('home_controller')); | |
} | |
return ['form' => $form->createView()]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class UserRegister extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name', 'text', ['label' => 'Enter your name']) | |
->add('email','text', ['label' => 'Enter your email']) | |
->add('password','password', ['label' => 'Enter your password']); | |
} | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
} | |
public function getName() | |
{ | |
return 'app_bundle_user_register'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment