How to install and configure FOSUserBundle with Symfony 2.5.*

When it comes to start multiple projects, configuring all the necessary vendors become an automatism.
Here is a guideline to help you configure new symfony projects with FOSUserBundle.

 
First, target FOSUserBundle dependency in your composer.json

"require": {
    "friendsofsymfony/user-bundle": "2.0.*@dev"
}

 
Install all your dependencies

php composer.phar update

 
Then, generate a new bundle we will name userBundle

php bin/console generate:bundle

 
In the userBundle.php generated file, extend FOSUserBundle like this

class UserBundle extends Bundle
 {
    public function getParent()
    {
      return 'FOSUserBundle';
    }
 }

 
We can now configure FOSUserBundle in our security.yml in “app/config”.
We will change default encoder

encoders:
  FOS\UserBundle\Model\UserInterface: sha512

 
We change the default provider

providers:       
    fos_userbundle:
        id: fos_user.user_provider.username

 
We then the new firewall of our application

main:
  pattern: ^/
     form_login:
         provider: fos_userbundle
         csrf_provider: form.csrf_provider
     logout:       true
     anonymous:    true

 
Once the firewall is configured, we have to secure our application urls

- { path: ^/(_(profiler|wdt)|css|images|js)/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, role: ROLE_ADMIN }

  
Ok, right, we have configured the basic security options and created a userBundle that extends FOSUserBundle.
But Symfony doesn’t actually know who is FOSUserBundle, and, our firewall doesn’t know where to find the user entity.

 
We have to call FOSUserBundle in the appKernel.

$bundles = array(
         new FOS\UserBundle\FOSUserBundle(),
 );

 
Generate an empty User entity.

php bin/console doctrine:generate:entity --entity=VendorUserBundle:User

 
in the User entity, extend FOSUserBundle and turn id attribute from private to protected.

<?php

namespace Vendor\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class User extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

 
Ok, now we must specify FOSUserBundle where to find the user entity in app/config/config.yml, and activate framework locale by uncommenting it.

fos_user:
  db_driver: orm
  firewall_name: main
  user_class: Vendor\UserBundle\Entity\User

framework:
    translator:      { fallback: "%locale%" }

 
To finish, just add FOSUserBundle routes in our app/config/routing.yml file…

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

 
And finally, upgrade your database schema

php bin/console doctrine:schema:update --dump-sql
php bin/console doctrine:schema:update --force

You have a ready to use FOSUserBundle installation.

Advertisements
How to install and configure FOSUserBundle with Symfony 2.5.*

2 thoughts on “How to install and configure FOSUserBundle with Symfony 2.5.*

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s