— tech du peupl

Language :

Language

Getting started with Symfony 2.1, SonataAdminBundle, FOSUserBundle and SonataUserBundle

How to set up a new Symfony 2.1 project using SonataAdminBundle, FOSUserBundle and SonataUserBundle. (Disclaimer : disclaimer is at the end of this post.)

First, let me say that Symfony, with these three bundles, is an incredibly useful base for lots of projects. FOSUserBundle provides user management. By default it will let you have a register form (for new users to register), a log in/out form, lost password form, etc. User management comes with group management, so that you can file users into groups, assign them different permissions, etc. SonataAdminBundle provides an admin interface to administrate your users and groups. It also creates a nice CRUD, with shiny nice jQuery options, everything wrapped in Bootstrap. SonataUserBundle makes the link between FOSUserBundle and SonataAdminBundle.

I will first create a new Symfony 2.1 project :
⚘ composer.phar create-project symfony/framework-standard-edition ./tech 2.1 

PRO-TIP :
If you haven't already done this and are tired of copying composer.phar in every new Symfony2 project, run this :
⚘ curl -s https://getcomposer.org/installer | sudo "php -- --install-dir=/usr/bin"
(Or do it in two separate steps, if you're suspicious. You have all right to be.)
It will allow you to run composer from anywhere : ⚘ composer.phar something something

Edit composer.json, add these dependencies to the "require" section :

and update the project :
⚘ composer.phar update 

Then edit AppKernel.php to add these :

and while you're at it, remove AcmeDemoBundle.

Next step is editing app/config/config.yml to add :

Now that we told FOSUserBundle to use Sonata's User entity, we have to create it :
⚘ php app/console sonata:easy-extends:generate SonataUserBundle 

This generates the class ApplicationSonataUserBundle in folder app/Application/Sonata/UserBundle. You have to move this Application folder from app/ to src/.

And of course, register this new Bunble in AppKernel.php :

Time to route everything :

Since we have user management, use it to secure the admin panel :

Now is time to update the database schema : (if you haven't configured your database yet, do it now in app/config/parameters.yml)
⚘ php app/console doctrine:schema:update --dump-sql
⚘ php app/console doctrine:schema:update --force
 

If you're getting this error :

[Doctrine\DBAL\DBALException]
Unknown column type "json" requested.

You'll have to add this to config.yml

Create some users :
⚘ php app/console fos:user:create admin test@example.com password --super-admin
⚘ php app/console fos:user:create testuser test@example.com password
 

At this point, if you log in on http://yourhost.com/your/app_dev.php/login (no trailing slash !) with your freshly created user or admin, you'll notice that the strings are not localized. By example : link_edit_profile. To fix this issue, create this new file : (you'll most probably have to create the translations folder as well) : src/Application/Sonata/UserBundle/Resources/translations/SonataUserBundle.en.yml with the following content :

Edit app/config/config.yml to uncomment the highlighted line : (translator, in framework section)

Don't forget to update your assets : php app/console assets:install --symlink 

Now you have a working setup of SonataAdminBundle, FOSUserBundle and SonataUserBundle with Symfony 2.1.

In some upcoming posts, I will show you how to build something with this nice setup.
[EDIT]Ok, here it is ! I wrote a 4-posts series about customizing SonataAdminBundle, using SonataMediaBundle for files and picture uploads, and a lot more fun stuff. Start here : Symfony 2.1 : ACL, SonataAdminBundle, SonataMediabundle, SonataUserBundle and FOSUserBundle[/EDIT]

Disclaimer / Important remark : This post is adapted from the great jorzekowsky, who wrote Setting up a Symfony2 Project with FOSUserBundle, SonataUserBundle and SonataAdminBundle. I shall kudos him ! Also, check out his other blog posts !

  • djacquel

    Bravo !
    Une fois les choses remises en ordre, c’est vrai que c’est simple d’installer le SonataUserBundle.
    Merci pour cet article.
    David

  • Nomzeb

    Merci, simple, facile, efficace…heureusement que ce post étais la !!!

  • Ivan

    This is a great article, it helped me a lot, thank you.

  • steph

    @djacquel

    J’ai une erreur avec composer FosUserBundle not found in AppKernel…

    Comment as tu remis les choses dans l’ordre?

  • victor

    @djacquel, Nomzeb & Ivan : Thanks for your kind words / Merci !

    @steph : Fais les choses dans l’ordre dans lequel je les explique. ;)
    Tu dois absolument d’abord modifier ton composer.json, puis mettre à jour ton projet avec composer, puis modifier AppKernel pour charger FOSUB.

  • http://hidefolders.ucoz.com Name

    Thanks. Very helpful article especially for beginners.

  • Glouzy201

    C’est super, je vous jure!
    Merci encore (y)

  • giorg

    Hi, when I give the command composer.phar update I get the following error:

    Problem 1
    – sonata-project/admin-bundle dev-master requires sonata-project/block-bundle 2.2.*@dev -> no matching package found.
    – sonata-project/admin-bundle dev-master requires sonata-project/block-bundle 2.2.*@dev -> no matching package found.
    – Installation request for sonata-project/admin-bundle dev-master -> satisfiable by sonata-project/admin-bundle dev-master.
    any suggestion? many thanks

  • http://www.facebook.com/zehi.ragheb.jbn Ragheb Zehi

    Fatal error: Class ‘ApplicationSonataUserBundleApplicationSonataUserBundle’ not found in C:wampwwwPaplocProjetSymfonyappAppKernel.php on line 36