This tutorial demonstrates how to integrate FOSUserBundle into a Symfony project for user authentication and management. FOSUserBundle simplifies user registration, login, password resets, and profile management, leveraging Symfony's security system.
Key Features of FOSUserBundle:
(Image: Homestead Improved Setup)
Setting up the Symfony Project:
This guide uses Homestead Improved. Add a new site to your Homestead.yml
file:
sites: - map: symfonylogin.app to: /home/vagrant/Code/SymfonyLogin/web type: symfony databases: - symfony
Add 192.168.10.10 symfonylogin.app
to your /etc/hosts
file. Boot the VM with vagrant up
.
Install the Symfony installer within the VM using vagrant ssh
and these commands:
curl -LsS http://symfony.com/installer > symfony sudo mv symfony /usr/local/bin/symfony chmod a+x /usr/local/bin/symfony
Create a new Symfony application:
cd Code symfony new SymfonyLogin
Configure parameters.yml
with your database and email settings. Access the skeleton application at http://symfonylogin.app
. (Note: For Vagrant, you may need to adjust app_dev.php
to include your host IP.)
Integrating FOSUserBundle:
Installation: Use Composer:
composer require friendsofsymfony/user-bundle "~2.0@dev"
Bundle Registration: In AppKernel.php
, add new FOSUserBundleFOSUserBundle()
to the $bundles
array.
Configuration (config.yml
):
Enable the translator:
translator: { fallbacks: ["%locale%"] }
Configure security in security.yml
:
security: encoders: AppBundle\Entity\User: bcrypt # ... (rest of security configuration)
Add FOSUserBundle configuration:
fos_user: db_driver: orm firewall_name: main user_class: AppBundle\Entity\User from_email: address: admin@example.com sender_name: Example.com registration: confirmation: enabled: true template: FOSUserBundle:Registration:email.txt.twig
User Entity (src/AppBundle/Entity/User.php
): Create a User entity extending FOSUserBundleModelUser
:
<?php // ... (Entity code as in original example) ?>
Update Database Schema:
php app/console doctrine:schema:update --force
Import Routes (routing.yml
):
fos_user: resource: "@FOSUserBundle/Resources/config/routing/all.xml"
(Image: Default Registration Form)
Customizing Templates:
Create app/Resources/FOSUserBundle/views/Registration/
and copy the necessary templates from vendor/friendsofsymfony/user-bundle/Resources/views/Registration/
. Modify these templates to customize the registration form's appearance. Example modifications to register.html.twig
and register_content.html.twig
are shown in the original example.
(Image: Customized Registration Form)
Further Customization:
app/Resources/translations/
.This enhanced summary provides a more concise and organized explanation of the FOSUserBundle integration process, while maintaining the key information and images from the original input. The FAQ section is omitted for brevity as the tutorial itself comprehensively addresses those points.
The above is the detailed content of Basic User Management in Symfony2 with FOSUserBundle. For more information, please follow other related articles on the PHP Chinese website!