Kritim Yantra
Apr 18, 2025
Symfony is a powerful PHP framework that makes it easy to build secure web applications. In this tutorial, we’ll create a complete login and registration system from scratch.
By the end, you’ll learn:
✅ How to set up a Symfony project
✅ Creating a User entity with Symfony’s security system
✅ Building registration and login forms
✅ Securing routes with authentication
Let’s get started!
First, make sure you have PHP 8+ and Composer installed.
composer create-project symfony/website-skeleton symfony-login-system
cd symfony-login-system
symfony serve
Visit http://localhost:8000
to see your Symfony app running.
Symfony uses Doctrine to interact with databases.
.env
file:DATABASE_URL="mysql://root:@127.0.0.1:3306/symfony_auth"
(Replace with your database credentials.)
php bin/console doctrine:database:create
Symfony provides a make:user
command to generate a secure User class.
php bin/console make:user
Follow the prompts:
User
This generates:
src/Entity/User.php
src/Repository/UserRepository.php
Add more fields (e.g., firstName
, lastName
):
// src/Entity/User.php
#[ORM\Column(length: 50)]
private ?string $firstName = null;
#[ORM\Column(length: 50)]
private ?string $lastName = null;
php bin/console make:migration
php bin/console doctrine:migrations:migrate
Symfony’s security-bundle
handles authentication.
php bin/console make:auth
Choose:
AppAuthenticator
SecurityController
This generates:
src/Security/AppAuthenticator.php
src/Controller/SecurityController.php
templates/security/login.html.twig
)php bin/console make:registration-form
This creates:
src/Controller/RegistrationController.php
templates/registration/register.html.twig
Edit src/Form/RegistrationFormType.php
to include firstName
and lastName
:
->add('firstName', TextType::class)
->add('lastName', TextType::class)
templates/registration/register.html.twig
:{{ form_start(registrationForm) }}
{{ form_row(registrationForm.firstName) }}
{{ form_row(registrationForm.lastName) }}
{{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.plainPassword) }}
<button type="submit">Register</button>
{{ form_end(registrationForm) }}
Edit config/packages/security.yaml
:
security:
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/profile, roles: ROLE_USER }
Now:
/login
→ Public /register
→ Public /profile
→ Only logged-in usersVisit http://localhost:8000/register
and fill out the form.
Go to http://localhost:8000/login
and enter credentials.
Try visiting /profile
—it should work only if logged in.
Improve UX with success/error messages.
RegistrationController.php
:$this->addFlash('success', 'Registration successful!');
base.html.twig
):{% for message in app.flashes('success') %}
<div class="alert alert-success">{{ message }}</div>
{% endfor %}
You’ve built a secure login & registration system in Symfony!
✅ Installed Symfony & set up a database
✅ Created a User
entity with Doctrine
✅ Generated login & registration forms
✅ Secured routes based on user roles
🔹 Add email verification
🔹 Implement password reset
🔹 Try OAuth (Google/Facebook login)
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google