FOSUserBundle customization notes


I’ve been busy creating a new project in which I use symfony2 for the first time. Working with symfony1.x for nearly 3 years I really like some of the ideas of symfony2. However there are also some things I dislike, or just don’t fully understand yet.

Here are some quick notes I took while reading the FOSBundle Documentation.


Default Templates

The easy and quick way:

  • Basically override the FOS Template by creating a file under ‘./app/Resources/FOSUserBundle/views/layout.html.twig’
  • Be sure to include this block, which will hold the FOSUserBundle Content. 
{% block fos_user_content %}
{% endblock fos_user_content %}

2. The more complicated way

  • Use this if you want to override their controllers.
  • I don’t need this for now. But you can of course read more under GIT FOSUserBundle Documentation (See section b).

Hooking into the controllers

Something about EventListener but I can’t find the classes in their Bundle? Did I miss something ? I’m pretty sure … I’ll look at that later.

Overriding Default FOSUserBundle Controllers

  • If your bundles name is ‘MyUserBundle’ then create a ./src/MyUserBundle/MyUserBundle.php which extends Bundle.
  • Then overwrite ‘getParent()’ and return ‘FOSUserBundle’.
  • !Note that one Bundle can only have one child!
  • Example if you want to override the RegistrationController:
    Create ./src/MyUserBundle/Controller/RegistrationController.php and implement a ‘registerAction()’.
    For more informations look

Overriding Default FOSUserBundle Forms

This is needed if you add some custom fields to the user class that implements the fos user bundle. As I have a user and a corresponding user profile, I’ll first need to figure out how to connect models in forms. So when a user signs up he gets to fill in:

  •  username, mail, etc. :: These will be stored in my user class.
  • surname, name, street, etc. :: These will be stored in the user profile.

As for the moment I have no clue on how to do that, I’ll have a look at it later on. 🙂

Using the UserManager
Some infos on how to use or override the userManager.

Command Line Tools
How to use cli commands for user management:

  • Create
  • De/Activate
  • Promote or Demote
  • Change Password

Transforming a username to a user in forms
Does what it says. HowTo transform username into user entity in forms.

How to send mails on certain user actions (register, reset, etc.)
This seems to be pretty cool because it uses twig.

Using the groups
Basically extend the BaseGroup and add foreign keys relationship to it.
This is a bit tricky because there seems to be a bug in Doctrine2.
It should not generate the methods “addGroup” and “removeGroup” to my User class.
See here for my problem description.
After solving or working around this problem, the rest is straight forward.

More about the Doctrine implementations
Infos on using a different object manager. If you don’t use Doctrine, read it.

Supplemental Documentation
Infos about how to use custom validators.

Replacing the canonicalizer
Infos on how one could disable the used canonicalization to prevent having usernames like “marcus” and “Marcus”.

Using a custom storage layer
Infos on how to design your own storage layer.

Configuration Reference
This is a very usefull reference for the whole FOSBundle. If you need a good example, this is the place to look at.

Adding invitations to registration
Good HowTo on adding invitation registration.

Leave a Reply