Getting Started With FOSUserBundle ================================== The Symfony2 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. 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. ## Prerequisites This version of the bundle requires Symfony 2.1+. If you are using Symfony 2.0.x, please use the 1.2.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. ``` yaml # app/config/config.yml framework: translator: ~ ``` For more information about translations, check [Symfony documentation](http://symfony.com/doc/current/book/translation.html). ## 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 Add FOSUserBundle in your composer.json: ```js { "require": { "friendsofsymfony/user-bundle": "*" } } ``` Now tell composer to download the bundle by running the command: ``` bash $ php composer.phar update friendsofsymfony/user-bundle ``` Composer will install the bundle to your project's `vendor/friendsofsymfony` directory. ### Step 2: Enable the bundle Enable the bundle in the kernel: ``` php When you extend from the mapped superclass provided by the bundle, don't > redefine the mapping for the other fields as it is provided by the bundle. In the following sections, you'll see examples of how your `User` class should look, depending on how you're storing your users (Doctrine ORM, MongoDB ODM, or CouchDB ODM). Your `User` class can live inside any bundle in your application. For example, if you work at "Acme" company, then you might create a bundle called `AcmeUserBundle` and place your `User` class in it. **Note:** > The doc uses a bundle named `AcmeUserBundle`. If you want to use the same > name, you need to register it in your kernel. But you can of course place > your user class in the bundle you want. **Warning:** > If you override the __construct() method in your User class, be sure > to call parent::__construct(), as the base User class depends on > this to initialize some fields. **a) Doctrine ORM User class** If you're persisting your users via the Doctrine ORM, then your `User` class should live in the `Entity` namespace of your bundle and look like this to start: ``` php `User` is a reserved keyword in SQL so you cannot use it as table name. **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: ``` php Although we have used the form login mechanism in this example, the FOSUserBundle > user provider is compatible with many other authentication methods as well. Please > read the Symfony2 Security component documention for more information on the > other types of authentication methods. The `access_control` section is where you specify the credentials necessary for users trying to access specific parts of your application. The bundle requires that the login form and all the routes used to create a user and reset the password be available to unauthenticated users but use the same firewall as the pages you want to secure with the bundle. This is why you have specified that the any request matching the `/login` pattern or starting with `/register` or `/resetting` have been made available to anonymous users. You have also specified that any request beginning with `/admin` will require a user to have the `ROLE_ADMIN` role. For more information on configuring the `security.yml` file please read the Symfony2 security component [documentation](http://symfony.com/doc/current/book/security.html). **Note:** > Pay close attention to the name, `main`, that we have given to the firewall which > the FOSUserBundle is configured in. You will use this in the next step when you > configure the FOSUserBundle. ### Step 5: Configure the FOSUserBundle Now that you have properly configured your application's `security.yml` to work with the FOSUserBundle, the next step is to configure the bundle to work with the specific needs of your application. Add the following configuration to your `config.yml` file according to which type of datastore you are using. ``` yaml # app/config/config.yml fos_user: db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel' firewall_name: main user_class: Acme\UserBundle\Entity\User ``` Or if you prefer XML: ``` xml ``` Only three configuration values are required to use the bundle: * The type of datastore you are using (`orm`, `mongodb`, `couchdb` or `propel`). * The firewall name which you configured in Step 5. * The fully qualified class name (FQCN) of the `User` class which you created in Step 4. **Warning:** > When using one of the Doctrine implementation, you need either to use the > `auto_mapping` option of the corresponding bundle (done by default for > DoctrineBundle in the standard distribution) or to activate the mapping > for FOSUserBundle otherwise the base mapping will be ignored. ### 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. In YAML: ``` yaml # app/config/routing.yml fos_user_security: resource: "@FOSUserBundle/Resources/config/routing/security.xml" fos_user_profile: resource: "@FOSUserBundle/Resources/config/routing/profile.xml" prefix: /profile fos_user_register: resource: "@FOSUserBundle/Resources/config/routing/registration.xml" prefix: /register fos_user_resetting: resource: "@FOSUserBundle/Resources/config/routing/resetting.xml" prefix: /resetting fos_user_change_password: resource: "@FOSUserBundle/Resources/config/routing/change_password.xml" prefix: /profile ``` Or if you prefer XML: ``` 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. ``` bash $ php app/console doctrine:schema:update --force ``` For MongoDB users you can run the following command to create the indexes. ``` bash $ php app/console doctrine:mongodb:schema:create --index ``` For Propel users you have to install the [TypehintableBehavior](https://github.com/willdurand/TypehintableBehavior) before to build your model. First, install it: ```json { "require": { "willdurand/propel-typehintable-behavior": "*" } } ``` You now can run the following command to create the model: ``` bash $ php app/console propel:build ``` > To create SQL, run the command `propel:build --insert-sql` or use migration > commands if you have an existing schema in your database. You now can login 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: - [Overriding Templates](overriding_templates.md) - [Overriding Controllers](overriding_controllers.md) - [Overriding Forms](overriding_forms.md) - [Using the UserManager](user_manager.md) - [Command Line Tools](command_line_tools.md) - [Logging by username or email](logging_by_username_or_email.md) - [Transforming a username to a user in forms](form_type.md) - [Emails](emails.md) - [Using the groups](groups.md) - [More about the Doctrine implementations](doctrine.md) - [Supplemental Documentation](supplemental.md) - [Replacing the canonicalizer](canonicalizer.md) - [Using a custom storage layer](custom_storage_layer.md) - [Configuration Reference](configuration_reference.md) - [Adding invitations to registration](adding_invitation_registration.md)