Sonata & FOS user as a base
SonataAdminBundle + FOSUserBundle: Have a GOOD base project
This article is going to explain /how to/ correctly create a backoffice
(admin panel + crud) in Symfony 2.
Generally, for any web project, you need a back office to manager
entities, data, users and groups and, fortunately today there are a
number of bundles for Symfony 2 which allow you to create an admin panel
quickly.
(.. because it is boring to redevelop phpmyadmin we can save ourselves
the effort and reutilise existing Symfony2 bundles)
What bundles do you need to install?
[FOSUserBundle] git=git://github.com/FriendsOfSymfony/FOSUserBundle.git target=bundles/FOS/UserBundle [SonatajQueryBundle] git=https://github.com/sonata-project/SonatajQueryBundle.git target=/bundles/Sonata/jQueryBundle [SonataAdminBundle] git=https://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=https://github.com/sonata-project/SonataDoctrineORMAdminBundle.git target=/bundles/Sonata/DoctrineORMAdminBundle
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\User
Run
php app/console sonata:easy-extends:generate SonataUserBundle
It is going to generate an Application UserBundle, but you can use your own.
To develop your understanding, do this by default and try after to readapt the code with your UserBundle
Add the new Bundle to app/AppKernel.php
// app/AppKernel.php public function registerbundles() { $bundles = array( // Application Bundles // ... new Application\Sonata\UserBundle\ApplicationSonataUserBundle(), // ... ); // ... }
Add routing
# 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 sonata_user_impersonating: pattern: / defaults: { _controller: SonataPageBundle:Page:catchAll }
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 } # ...
Do theses commands for the end
php app/console doctrine:schema:update --force php app/console assets:install web php app/console cache:clear php app/console fos:user:create admin admin@example.com password --super-admin
You need to enable your translator in /app/config.yml
framework: translator: ~
That’s it!!!
As you can see, a lotof things must be done for it to
work
But after this initial step you will only need to modify the XML files
and the Admin class ((plural?)) of your new bundles.
With this demo you should have a functioning admin interface for « user »
and « group » entities. This is accomplished only with the following 2
files:
– Setup UserAdmin Class (found in AdminFolder in the bundle SonataUserBundle) (see more info in SonataAdminBundle Doc)
– Setup admin_orm.xml (found in Ressources/coinfig in the bundle SonataUserBundle) (see more info in SonataAdminBundle Doc)
When you need to administer more entities in your application, editing
these two files is all you will need to do.
For more information, see: http://sonata-project.org/bundles/admin/master/doc/index.html