vendor/bundles/FOS/UserBundle/Resources/doc/index.md
author ymh <ymh.work@gmail.com>
Sun, 06 Nov 2011 23:44:37 +0100
changeset 27 1df556b2c0f9
parent 3 e54dfe4d0b2b
permissions -rwxr-xr-x
Correct memory problem
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
Getting Started With FOSUserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
==================================
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
The Symfony2 security component provides a flexible security framework that
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
allows you to load users from configuration, a database, or anywhere else
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
you can imagine. The FOSUserBundle builds on top of this to make it quick
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
and easy to store users in a database.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
So, if you need to persist and fetch the users in your system to and from
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
a database, then you're in the right place.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
## Prerequisites
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
### Translations
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
If you wish to use default texts provided in this bundle, you have to make
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
sure you have translator enabled in your config.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
# app/config/config.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
framework:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    translator: ~
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
For more information about translations, check [Symfony documentation](http://symfony.com/doc/2.0/book/translation.html).
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
## Installation
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
Installation is a quick (I promise!) 8 step process:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
1. Download FOSUserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
2. Configure the Autoloader
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
3. Enable the Bundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
4. Create your User class
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
5. Configure your application's security.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
6. Configure the FOSUserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
7. Import FOSUserBundle routing
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
8. Update your database schema
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
### Step 1: Download FOSUserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
Ultimately, the FOSUserBundle files should be downloaded to the
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
`vendor/bundles/FOS/UserBundle` directory.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
This can be done in several ways, depending on your preference. The first
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
method is the standard Symfony2 method.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
**Using the vendors script**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
Add the following lines in your `deps` file:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
[FOSUserBundle]
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    git=git://github.com/FriendsOfSymfony/FOSUserBundle.git
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    target=bundles/FOS/UserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
Now, run the vendors script to download the bundle:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
``` bash
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
$ php bin/vendors install
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
**Using submodules**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
If you prefer instead to use git submodules, the run the following:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
``` bash
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
$ git submodule add git://github.com/FriendsOfSymfony/FOSUserBundle.git vendor/bundles/FOS/UserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
$ git submodule update --init
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
### Step 2: Configure the Autoloader
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
Add the `FOS` namespace to your autoloader:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
// app/autoload.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
$loader->registerNamespaces(array(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    // ...
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    'FOS' => __DIR__.'/../vendor/bundles',
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
));
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
### Step 3: Enable the bundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
Finally, enable the bundle in the kernel:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
// app/AppKernel.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
public function registerBundles()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
    $bundles = array(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        // ...
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        new FOS\UserBundle\FOSUserBundle(),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
    );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
### Step 4: Create your User class
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
The goal of this bundle is to persist some `User` class to a database (MySql,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
MongoDB, CouchDB, etc). Your first job, then, is to create the `User` class
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
for your application. This class can look and act however you want: add any
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
properties or methods you find useful. This is *your* `User` class.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
This class has just two requirements, which allow it to take advantage of
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
all of the functionality in the FOSUserBundle:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
1. It must extend one of the base `User` classes from the bundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
2. It must have an `id` field
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
In the following sections, you'll see examples of how your `User` class should
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
look, depending on how you're storing your users (Doctrine ORM, MongoDB ODM,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
or CouchDB ODM).
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
Your `User` class can live inside any bundle in your application. For example,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
if you work at "Acme" company, then you might create a bundle called `AcmeUserBundle`
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
and place your `User` class in it.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
**Warning:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
> If you override the __construct() method in your User class, be sure
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
> to call parent::__construct(), as the base User class depends on
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
> this to initialize some fields.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
**a) Doctrine ORM User class**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
If you're persisting your users via the Doctrine ORM, then your `User` class
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
should live in the `Entity` namespace of your bundle and look like this to
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
start:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
// src/Acme/UserBundle/Entity/User.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
namespace Acme\UserBundle\Entity;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
use FOS\UserBundle\Entity\User as BaseUser;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
use Doctrine\ORM\Mapping as ORM;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
/**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
 * @ORM\Entity
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
 * @ORM\Table(name="fos_user")
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
class User extends BaseUser
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
     * @ORM\Id
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
     * @ORM\Column(type="integer")
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
     * @ORM\GeneratedValue(strategy="AUTO")
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
    protected $id;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
    public function __construct()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        parent::__construct();
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        // your own logic
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
> `User` is a reserved keyword in SQL so you cannot use it as table name.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
**b) MongoDB User class**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
If you're persisting your users via the Doctrine MongoDB ODM, then your `User`
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
class should live in the `Document` namespace of your bundle and look like
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
this to start:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
// src/Acme/UserBundle/Document/User.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
namespace Acme\UserBundle\Document;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
use FOS\UserBundle\Document\User as BaseUser;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
/**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
 * @MongoDB\Document
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
class User extends BaseUser
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
     * @MongoDB\Id(strategy="auto")
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    protected $id;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
    public function __construct()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
        parent::__construct();
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
        // your own logic
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
**c) CouchDB User class**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
If you're persisting your users via the Doctrine CouchDB ODM, then your `User`
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
class should live in the `Document` namespace of your bundle and look like
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
this to start:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
``` php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
// src/Acme/UserBundle/Document/User.php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
namespace Acme\UserBundle\Document;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
use FOS\UserBundle\Document\User as BaseUser;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
use Doctrine\ODM\CouchDB\Mapping as CouchDB;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
/**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
 * @CouchDB\Document
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
class User extends BaseUser
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
     * @CouchDB\Id
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
    protected $id;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    public function __construct()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
        parent::__construct();
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        // your own logic
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
}
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
### Step 5: Configure your application's security.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
In order for Symfony's security component to use the FOSUserBundle, you must
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
tell it to do so in the `security.yml` file. The `security.yml` file is where the
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
basic configuration for the security for your application is contained.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
Below is a minimal example of the configuration necessary to use the FOSUserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
in your application:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
``` yaml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
# app/config/security.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
security:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
    providers:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        fos_userbundle:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
            id: fos_user.user_manager
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
    firewalls:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
        main:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
            pattern: ^/
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
            form_login:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
                provider: fos_userbundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
            logout:       true
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
            anonymous:    true
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
    access_control:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        - { path: ^/admin/, role: ROLE_ADMIN }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
    role_hierarchy:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
        ROLE_ADMIN:       ROLE_USER
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
        ROLE_SUPER_ADMIN: ROLE_ADMIN
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
Under the `providers` section, you are making the bundle's packaged user provider
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
service available via the alias `fos_userbundle`. The id of the bundle's user
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
provider service is `fos_user.user_manager`.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
Next, take a look at examine the `firewalls` section. Here we have declared a
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
firewall named `main`. By specifying `form_login`, you have told the Symfony2
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
framework that any time a request is made to this firewall that leads to the
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
user needing to authenticate himself, the user will be redirected to a form
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
where he will be able to enter his credentials. It should come as no surprise
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
then that you have specified the user provider we declared earlier as the
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
provider for the firewall to use as part of the authentication process.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
> Although we have used the form login mechanism in this example, the FOSUserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
> user provider is compatible with many other authentication methods as well. Please
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
> read the Symfony2 Security component documention for more information on the
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
> other types of authentication methods.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
The `access_control` section is where you specify the credentials necessary for
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
users trying to access specific parts of your application. The bundle requires
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
that the login form and all the routes used to create a user and reset the password
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
be available to unauthenticated users but use the same firewall as
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
the pages you want to secure with the bundle. This is why you have specified that
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
the any request matching the `/login` pattern or starting with `/register` or
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
`/resetting` have been made available to anonymous users. You have also specified
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
that any request beginning with `/admin` will require a user to have the
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
`ROLE_ADMIN` role.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
For more information on configuring the `security.yml` file please read the Symfony2
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
security component [documentation](http://symfony.com/doc/current/book/security.html).
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
> Pay close attention to the name, `main`, that we have given to the firewall which
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
> the FOSUserBundle is configured in. You will use this in the next step when you
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
> configure the FOSUserBundle.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
### Step 6: Configure the FOSUserBundle
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
Now that you have properly configured your application's `security.yml` to work
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
with the FOSUserBundle, the next step is to configure the bundle to work with
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
the specific needs of your application.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
Add the following configuration to your `config.yml` file according to which type
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
of datastore you are using.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
``` yaml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
# app/config/config.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
fos_user:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
    db_driver: orm # other valid values are 'mongodb', 'couchdb'
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
    firewall_name: main
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
    user_class: Acme\UserBundle\Entity\User
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
Or if you prefer XML:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
``` xml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
# app/config/config.xml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
<!-- app/config/config.xml -->
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
<!-- other valid 'db-driver' values are 'mongodb' and 'couchdb' -->
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
<fos_user:config
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
    db-driver="orm"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
    firewall-name="main"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
    user-class="Acme\UserBundle\Entity\User"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
/>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
Only three configuration values are required to use the bundle:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
* The type of datastore you are using (`orm`, `mongodb`, or `couchdb`).
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
* The firewall name which you configured in Step 5.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
* The fully qualified class name (FQCN) of the `User` class which you created in Step 2
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
### Step 7: Import FOSUserBundle routing files
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
Now that you have activated and configured the bundle, all that is left to do is
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
import the FOSUserBundle routing files.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
By importing the routing files you will have ready made pages for things such as
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
logging in, creating users, etc.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
In YAML:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
``` yaml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
# app/config/routing.yml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
fos_user_security:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
fos_user_profile:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
    prefix: /profile
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
fos_user_register:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
    prefix: /register
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
fos_user_resetting:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
    prefix: /resetting
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
fos_user_change_password:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
    prefix: /change-password
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
Or if you prefer XML:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
``` xml
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
<!-- app/config/routing.xml -->
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
<import resource="@FOSUserBundle/Resources/config/routing/security.xml"/>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
<import resource="@FOSUserBundle/Resources/config/routing/profile.xml" prefix="/profile" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
<import resource="@FOSUserBundle/Resources/config/routing/registration.xml" prefix="/register" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
<import resource="@FOSUserBundle/Resources/config/routing/resetting.xml" prefix="/resetting" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
<import resource="@FOSUserBundle/Resources/config/routing/change_password.xml" prefix="/change-password" />
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
**Note:**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
> In order to use the built-in email functionality (confirmation of the account,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
> resetting of the password), you must activate and configure the SwiftmailerBundle.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
### Step 8: Update your database schema
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
Now that the bundle is configured, the last thing you need to do is update your
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
database schema because you have added a new entity, the `User` class which you
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
created in Step 2.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
For ORM run the following command.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
``` bash
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
$ php app/console doctrine:schema:update --force
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
For MongoDB users you can run the following command to create the indexes.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
``` bash
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
$ php app/console doctrine:mongodb:schema:create --index
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
```
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
You now can login at `http://app.com/app_dev.php/login`!
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
### Next Steps
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
Now that you have completed the basic installation and configuration of the
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
FOSUserBundle, you are ready to learn about more advanced features and usages
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
of the bundle.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
The following documents are available:
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
1. [Overriding Templates](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
2. [Overriding Controllers](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
3. [Overriding Forms](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
4. [Command Line Tools](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/command_line_tools.md)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
5. [Supplemental Documenation](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/supplemental.md)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
6. [Configuration Reference](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/configuration_reference.md)