Getting Started With FOSUserBundle ================================== The Symfony Security component provides a flexible security framework that allows you to load users from configuration, a database, or anywhere else you can imagine. The FOSUserBundle builds on top of this to make it quick and easy to store users in a database, as well as functionality for registration, reset password and a profile page. So, if you need to persist and fetch the users in your system to and from a database, then you're in the right place. For a video tutorial, check out `FOSUserBundle FTW`_ by KnpUniversity. Prerequisites ------------- This version of the bundle requires Symfony 2.8+. If you are using an older Symfony version, please use the 1.3.x releases of the bundle. Translations ~~~~~~~~~~~~ If you wish to use default texts provided in this bundle, you have to make sure you have translator enabled in your config. .. code-block:: yaml # app/config/config.yml framework: translator: ~ For more information about translations, check `Symfony documentation`_. Installation ------------ Installation is a quick (I promise!) 7 step process: 1. Download FOSUserBundle using composer 2. Enable the Bundle 3. Create your User class 4. Configure your application's security.yml 5. Configure the FOSUserBundle 6. Import FOSUserBundle routing 7. Update your database schema Step 1: Download FOSUserBundle using composer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Require the bundle with composer: .. code-block:: bash $ composer require friendsofsymfony/user-bundle "~2.0" Composer will install the bundle to your project's ``vendor/friendsofsymfony/user-bundle`` directory. If you encounter installation errors pointing at a lack of configuration parameters, such as ``The child node "db_driver" at path "fos_user" must be configured``, you should complete the configuration in Step 5 first and then re-run this step. Step 2: Enable the bundle ~~~~~~~~~~~~~~~~~~~~~~~~~ Enable the bundle in the kernel:: .. caution:: ``user`` is a reserved keyword in the SQL standard. If you need to use reserved words, surround them with backticks, *e.g.* ``@ORM\Table(name="`user`")`` b) MongoDB User class ..................... If you're persisting your users via the Doctrine MongoDB ODM, then your ``User`` class should live in the ``Document`` namespace of your bundle and look like this to start:: Only four configuration's node are required to use the bundle: * The type of datastore you are using (``orm``, ``mongodb`` or ``couchdb``). * The firewall name which you configured in Step 4. * The fully qualified class name (FQCN) of the ``User`` class which you created in Step 3. * The default email address to use when the bundle send a registration confirmation to the user. .. note:: FOSUserBundle uses a compiler pass to register mappings for the base User and Group model classes with the object manager that you configured it to use. (Unless specified explicitly, this is the default manager of your doctrine configuration.) Step 6: Import FOSUserBundle routing files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Now that you have activated and configured the bundle, all that is left to do is import the FOSUserBundle routing files. By importing the routing files you will have ready made pages for things such as logging in, creating users, etc. .. configuration-block:: .. code-block:: yaml # app/config/routing.yml fos_user: resource: "@FOSUserBundle/Resources/config/routing/all.xml" .. code-block:: xml .. note:: In order to use the built-in email functionality (confirmation of the account, resetting of the password), you must activate and configure the SwiftmailerBundle. Step 7: Update your database schema ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Now that the bundle is configured, the last thing you need to do is update your database schema because you have added a new entity, the ``User`` class which you created in Step 4. For ORM run the following command. .. code-block:: bash $ php bin/console doctrine:schema:update --force For MongoDB users you can run the following command to create the indexes. .. code-block:: bash $ php bin/console doctrine:mongodb:schema:create --index .. note:: If you use the Symfony 2.x structure in your project, use ``app/console`` instead of ``bin/console`` in the commands. You now can log in at ``http://app.com/app_dev.php/login``! Next Steps ~~~~~~~~~~ Now that you have completed the basic installation and configuration of the FOSUserBundle, you are ready to learn about more advanced features and usages of the bundle. The following documents are available: .. toctree:: :maxdepth: 1 overriding_templates controller_events overriding_forms user_manager command_line_tools logging_by_username_or_email form_type emails groups doctrine overriding_validation canonicalizer custom_storage_layer routing configuration_reference adding_invitation_registration .. _security component documentation: https://symfony.com/doc/current/book/security.html .. _Symfony documentation: https://symfony.com/doc/current/book/translation.html .. _TypehintableBehavior: https://github.com/willdurand/TypehintableBehavior .. _FOSUserBundle FTW: https://knpuniversity.com/screencast/fosuserbundle