Setting up a Symfony2 Project with FOSUserBundle, SonataUserBundle and SonataAdminBundle
Tested with Symfony Standard 2.0.7 (http://symfony.com/download?v=Symfony_Standard_Vendors_2.0.7.tgz)
This is basically a merge of the installation-guides of FOSUserBundle (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md), SonataAdminBundle (http://sonata-project.org/bundles/admin/master/doc/reference/installation.html) and SonataUserBundle (http://sonata-project.org/bundles/user/master/doc/reference/installation.html)
- Add
[FOSUserBundle] git=git://github.com/FriendsOfSymfony/FOSUserBundle.git target=bundles/FOS/UserBundle [SonatajQueryBundle] git=http://github.com/sonata-project/SonatajQueryBundle.git target=/bundles/Sonata/jQueryBundle [SonataAdminBundle] git=http://github.com/sonata-project/SonataAdminBundle.git target=/bundles/Sonata/AdminBundle [MenuBundle] git=https://github.com/KnpLabs/KnpMenuBundle.git target=/bundles/Knp/Bundle/MenuBundle [KnpMenu] git=https://github.com/KnpLabs/KnpMenu.git target=/knp/menu [SonataUserBundle] git=git://github.com/sonata-project/SonataUserBundle.git target=/bundles/Sonata/UserBundle [SonataEasyExtendsBundle] git=git://github.com/sonata-project/SonataEasyExtendsBundle.git target=/bundles/Sonata/EasyExtendsBundle [SonataDoctrineORMAdminBundle] git=http://github.com/sonata-project/SonataDoctrineORMAdminBundle.git target=/bundles/Sonata/DoctrineORMAdminBundleto your deps file. (Don’t forget the trailing newline.)
- Run
php bin/vendors install --reinstall
- Add the namespaces to app/autoload.php
// app/autoload.php $loader->registerNamespaces(array( // ... 'FOS' => __DIR__.'/../vendor/bundles', 'Sonata' => __DIR__.'/../vendor/bundles', 'Application' => __DIR__, 'Knp' => array( __DIR__.'/../vendor/bundles', __DIR__.'/../vendor/knp/menu/src', ), // ... )); - Enable the bundles in app/AppKernel.php
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FOS\UserBundle\FOSUserBundle(), new Sonata\jQueryBundle\SonatajQueryBundle(), new Sonata\AdminBundle\SonataAdminBundle(), new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(), new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'), new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(), // ... ); // ... } - Add
# app/config/config.yml fos_user: db_driver: orm firewall_name: main user_class: Application\Sonata\UserBundle\Entity\Userto app/config/config.yml
- Run
php app/console sonata:easy-extends:generate SonataUserBundle
- Add the new Bundle to app/AppKernel.php
// app/AppKernel.php public function registerbundles() { $bundles = array( // Application Bundles // ... new Application\Sonata\UserBundle\ApplicationSonataUserBundle(), // ... ); // ... } - Add
# 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: /change-password admin: resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml' prefix: /admin _sonata_admin: resource: . type: sonata_admin prefix: /admin soanata_user: resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml' prefix: /admin
to app/config/routing.yml
- Add the following to app/config/security.yml
# app/config/security.yml security: encoders: FOS\UserBundle\Model\UserInterface: sha512 role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] SONATA: - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT # if you are not using acl then this line must be uncommented providers: fos_userbundle: id: fos_user.user_manager firewalls: # -> custom firewall for the admin area of the URL admin: pattern: /admin(.*) form_login: provider: fos_userbundle login_path: /admin/login use_forward: false check_path: /admin/login_check failure_path: null logout: path: /admin/logout anonymous: true # -> end custom configuration # defaut login area for standard users main: pattern: .* form_login: provider: fos_userbundle login_path: /login use_forward: false check_path: /login_check failure_path: null logout: true anonymous: true # ... access_control: # URL of FOSUserBundle which need to be available to anonymous users - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } # -> custom access control for the admin area of the URL - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # -> end - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } # Secured part of the site # This config requires being logged for the whole site and having the admin role for the admin part. # Change these rules to adapt them to your needs - { path: ^/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] } - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY } # ... - Update your DB-Schema. Run
php app/console doctrine:schema:update --force
- Now, install the assets from the different bundles:
php app/console assets:install web
- Clear your cache:
php app/console cache:clear
- You can create an admin-user and a test-user with the following commands.
php app/console fos:user:create admin admin@example.com password --super-admin
php app/console fos:user:create testuser test@example.com password
- You need to enable your translator in /app/config.yml.
- Create app/Application/Sonata/UserBundle/Recources/translations/SonataUserBundle.en.ymlwith the following content:
# app/Application/Sonata/UserBundle/Recources/translations/SonataUserBundle.en.yml form: label_username: Username label_email: Email label_plain_password: Password (Plain) label_groups: Groups label_roles: Roles label_locked: Locked label_expired: Expired label_enabled: Enabled label_credentials_expired: Credentials Expired label_name: Name list: label_username: Username label_email: Email label_enabled: Enabled label_locked: Locked label_created_at: Created At label_roles: Roles label_name: Name filter: label_username: Username label_locked: Locked label_email: Email label_id: ID label_name: Name