Hello Friends,
This is my second post to build Symfony admin panel using sonata admin bundles. In this post we will create basic administrator panel for Symfony 2.2 project with authentication, user and group management. So let's start steps for it.
1 - Download & setup
Download Symfony 2.2 package with vendor and configure project with proper write permission for cache, logs directory & vhost to access package in browser.
2 - Check version of your Symfony package
> php app/console -V > Symfony version 2.2.3 - app/dev/debug
3 - Install required bundles
Install sonata-project/block-bundle
> php composer.phar require sonata-project/block-bundle:2.2.3
Install sonata-project/jquery-bundle
> php composer.phar require sonata-project/jquery-bundle:1.8.0
Install knplabs/knp-menu
> php composer.phar require knplabs/knp-menu:1.1.2
Install knplabs/knp-menu-bundle
> php composer.phar require knplabs/knp-menu-bundle:1.1.2
Install sonata-project/exporter
> php composer.phar require sonata-project/exporter:1.3.0
Install sonata-project/admin-bundle
> php composer.phar require sonata-project/admin-bundle:2.2.2
Install sonata-project/doctrine-orm-admin-bundle
> php composer.phar require sonata-project/doctrine-orm-admin-bundle:2.2.*@dev
Install sonata-project/intl-bundle
> php composer.phar require sonata-project/intl-bundle:2.1.*
4 - Alter your config.yml and add following configurations.
# app/config/config.yml # Sonata block Configuration sonata_block: default_contexts: [cms] blocks: sonata.admin.block.admin_list: contexts: [admin] #sonata.admin_doctrine_orm.block.audit: # contexts: [admin] sonata.block.service.text: sonata.block.service.rss: # knp menu configuration (optional) knp_menu: twig: template: knp_menu.html.twig templating: false default_renderer: twig # Translator configuration framework: translator: ~ # Sonata intl configuration sonata_intl: timezone: # default timezone used as fallback default: Europe/Paris # locale specific overrides locales: fr: Europe/Paris en_UK: Europe/London
5 - Be sure to enable these bundles in your AppKernel.php file.
// app/AppKernel.php public function registerBundles() { return array( // ... new Sonata\BlockBundle\SonataBlockBundle(), new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Sonata\jQueryBundle\SonatajQueryBundle(), new Sonata\AdminBundle\SonataAdminBundle(), new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(), new Sonata\IntlBundle\SonataIntlBundle(), // ... ); }
6 - Install the assets from the bundles
> php app/console assets:install web
7 - Now lets add routes for sonata admin bundle.
# app/config/routing.yml admin: resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml' prefix: /admin _sonata_admin: resource: . type: sonata_admin prefix: /admin
8 - Delete cache files
> php app/console cache:clear
9 - Install FOS & sonata-project/user-bundle
9-A :Install friendsofsymfony/user-bundle
> php composer.phar require friendsofsymfony/user-bundle:1.3.2
9-B :Install sonata-project/user-bundle
> php composer.phar require sonata-project/user-bundle:2.1.1
10 - Be sure to enable these bundles in your AppKernel.php file.
// app/AppKernel.php public function registerBundles() { return array( // ... new Sonata\BlockBundle\SonataBlockBundle(), new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Sonata\jQueryBundle\SonatajQueryBundle(), new Sonata\AdminBundle\SonataAdminBundle(), new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(), new Sonata\IntlBundle\SonataIntlBundle(), new FOS\UserBundle\FOSUserBundle(), new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'), new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(), // ... ); }
11 - Alter config.yml and add configurations for FOS user bundle.
# app/config/config.yml # FOS user bundle configuration fos_user: db_driver: orm firewall_name: main user_class: Application\Sonata\UserBundle\Entity\User
12 - Generate & configure User bundle
12-A : Generate user bundle
This bundle will gives an interface to manage users in application. Execute below command to generate User bundle for you by extending SontaUserBundle.
> php app/console sonata:easy-extends:generate SonataUserBundle
12-B : Register generated user bundle in autoload.php
Add below line in autoload.php
$loader->add('Application', __DIR__);Your autoload.php file looks like below
<?php use Doctrine\Common\Annotations\AnnotationRegistry; use Composer\Autoload\ClassLoader; /** * @var $loader ClassLoader */ $loader = require __DIR__.'/../vendor/autoload.php'; // intl if (!function_exists('intl_get_error_code')) { require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php'; } $loader->add('Application', __DIR__); AnnotationRegistry::registerLoader(array($loader, 'loadClass')); return $loader;
12-C : Register User bundle in app/AppKernel.php
Add below line to register generated user bundle.
new Application\Sonata\UserBundle\ApplicationSonataUserBundle()
// app/AppKernel.php public function registerBundles() { return array( // ... new Sonata\BlockBundle\SonataBlockBundle(), new Knp\Bundle\MenuBundle\KnpMenuBundle(), new Sonata\jQueryBundle\SonatajQueryBundle(), new Sonata\AdminBundle\SonataAdminBundle(), new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(), new Sonata\IntlBundle\SonataIntlBundle(), new FOS\UserBundle\FOSUserBundle(), new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'), new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(), new Application\Sonata\UserBundle\ApplicationSonataUserBundle(), // ... ); }
12- D : Add fos and sonata user routes in app/routing.yml
#app/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 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 soanata_user: resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml' prefix: /admin sonata_user_impersonating: pattern: / defaults: { _controller: SonataPageBundle:Page:catchAll }
12-E : Clear cache
> php app/console cache:clear
13 - Update db schema
> php app/console doctrine:schema:update --forceNOTE : if symfony give 'Unknown column type "json" requested.......' error, add custom type in Doctrine configuration, see below code
# app/config/config.yml doctrine: dbal: #for SonataNotificationBundle types: json: Sonata\Doctrine\Types\JsonType #----
14 - Add sonata admin configuration
#app/config/config.yml sonata_admin: title: <your application title> title_logo: /path/to/logo.png dashboard: blocks: # # display a dashboard block - { position: left, type: sonata.admin.block.admin_list }
15 - Lets create super admin user
All configurations completed, So lets create super admin user for our newly created Symfony 2.2 admin panel.
> php app/console fos:user:create <username> <email> <password> --super-admin
Now you can access your admin panel using below url
URL : http://yourdomain/app_dev.php/admin/dashboard
16 - Bundles url
https://github.com/sonata-project/SonataBlockBundle
https://github.com/sonata-project/SonatajQueryBundle
https://github.com/KnpLabs/KnpMenu
https://github.com/KnpLabs/KnpMenuBundle
https://github.com/sonata-project/exporter
https://github.com/sonata-project/SonataAdminBundle
https://github.com/sonata-project/SonataDoctrineORMAdminBundle
https://github.com/sonata-project/SonataIntlBundle
Now, our fully functional Symfony2.2 admin panel is ready.
Please post your comments, share my post and join this blog.
Rajesh Meniya
Symfony Developer
Step 6 . I had a issue :
ReplyDeleteFatal error: Class 'Sonata\AdminBundle\DependencyInjection\AbstractSonataAdminExtension' not found in /home/local/PYCO/hien.le/workspace/sonata/path/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/DependencyInjection/SonataDoctrineORMAdminExtension.php on line 29
hello hiren
ReplyDeletego to here https://github.com/sonata-project/SonataAdminBundle and download adminbundle and replace it with your sonata admin bundle in your vendor directory so your problem is solved
Hi there, I carried on the process and tried setting up the userBundle profile dashboard. By following the settings as written on the Sonata website however I keep getting this error:
ReplyDelete[InvalidArgumentException]
Unable to find file "@SonataUserBundle/Resources/config/routing/profile.xml
Have been fiddling around inside for hours now and cannot seem to find why? Google has nothing to say about this as far as I can tell. Been everywhere...
Any suggestions?
sf 2.3.9 - Was working perfectly fine till I started trying to access the Profile page, initially it was erroring saying no route found, so searched through and added the route's etc followed everything mentioned on :
http://sonata-project.org/bundles/user/master/doc/reference/profile_edition.html
from page 1 - 6. So frustrated now just wish I knew where I'd gone wrong.
Hi Doug,
DeleteHave look here and compare your configurations with my configured and working symfony+sonata sandbox
Rajesh Meniya
sandbox at github
Deletehttps://github.com/rajeshmeniya/Symfony2.2-sonata-admin
Have to add _1 to the .xml so it is profile_1.xml. hope this can help anyone else in the same position.
ReplyDelete