Monday, August 12, 2013

Sonata Admin Installation for symfony 2.2

OPTION 1

# composer.json

         "php": ">=5.3.3",
        "symfony/symfony": "2.2.*",
        "doctrine/orm": "~2.2,>=2.2.3",
        "doctrine/doctrine-bundle": "1.2.*",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "2.3.*",
        "symfony/swiftmailer-bundle": "2.2.*",
        "symfony/monolog-bundle": "2.2.*",
        "sensio/distribution-bundle": "2.2.*",
        "sensio/framework-extra-bundle": "2.2.*",
        "sensio/generator-bundle": "2.3.*",
        "jms/security-extra-bundle": "1.4.*",
        "jms/di-extra-bundle": "1.3.*",
        "doctrine/data-fixtures": "*",
        "doctrine/doctrine-fixtures-bundle": "*",
        "friendsofsymfony/user-bundle": "*",
        "knplabs/knp-paginator-bundle": "*",
        "payum/payum-bundle": "*",
        "payum/paypal-express-checkout-nvp": "*",
            "doctrine/doctrine-migrations-bundle": "dev-master",
            "doctrine/migrations": "dev-master",
            "symfony/console": "2.2.*@dev",
            "sonata-project/admin-bundle": "dev-master",
            "sonata-project/doctrine-orm-admin-bundle": "dev-master",
            "sonata-project/intl-bundle": "2.1.*",
            "sonata-project/cache-bundle": "2.*",
            "sonata-project/block-bundle": "2.2.*@dev",
            "simplethings/entity-audit-bundle": "dev-master",
            "knplabs/knp-menu-bundle":"1.1.x-dev"



# app/AutoLoad

# app/AppKernel

            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new JMS\AopBundle\JMSAopBundle(),
//            new Acme\HelloBundle\AcmeHelloBundle(),
            new ASF\PembedaBundle\ASFPembedaBundle(),
            new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
//            new Payum\Bundle\PayumBundle\PayumBundle(),
//            new Blogger\BlogBundle\BloggerBlogBundle(),
//            new Doctrine\Bundle\DoctrineMigrationsBundle\DoctrineMigrationsBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
            new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
            new JMS\DiExtraBundle\JMSDiExtraBundle($this),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
            new FOS\UserBundle\FOSUserBundle(),
            new Sonata\BlockBundle\SonataBlockBundle(),
            new Sonata\jQueryBundle\SonatajQueryBundle(),
            new Sonata\AdminBundle\SonataAdminBundle(),
            new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
            new SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle(),
            new Knp\Bundle\MenuBundle\KnpMenuBundle(),

# app/config/config.yml
sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]

        sonata.block.service.text: ~
        sonata.block.service.rss: ~

# app/config/config.yml

install the assets from the bundles: php app/console assets:install web
delete your cache: php app/console cache:clear


 OPTION 2

1. Download Symfony 2.2 package
2. Check version : php app/console -V
3. Install require bundles :
 php composer.phar require sonata-project/block-bundle:2.2.3
 php composer.phar require sonata-project/jquery-bundle:1.8.0 
 php composer.phar require knplabs/knp-menu:1.1.2
 php composer.phar require knplabs/knp-menu-bundle:1.1.2
 php composer.phar require sonata-project/exporter:1.3.0
 php composer.phar require sonata-project/admin-bundle:2.2.2
 php composer.phar require sonata-project/doctrine-orm-admin-bundle:2.2.*@dev
 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. AppKernel.php
    // 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.  app/config/routing.yml
     # 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
  php composer.phar require friendsofsymfony/user-bundle:1.3.2
  php composer.phar require sonata-project/user-bundle:2.1.1 
  
10 . app/AppKernel.php
 
    // 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. app/config/config.yml

    # 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 :

php app/console sonata:easy-extends:generate SonataUserBundle
 
add to autoload.php :  $loader->add('Application', __DIR__);
 
        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; 
 
Register User bundle in app/AppKernel.php : 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(),
        // ...
        );
    }
 
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 }
 
clear cache : php app/console cache:clear
 
13. update schema : php app/console doctrine:schema:update --force   
 
 NOTE : 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 - Create super admin user :

php app/console fos:user:create <username> <email> <password>
 
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 
 
 
 
 
  Option 3
 
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)
  1. Add
    [FOSUserBundle]
        target=bundles/FOS/UserBundle
     
    [SonatajQueryBundle]
        target=/bundles/Sonata/jQueryBundle
     
    [SonataAdminBundle]
        target=/bundles/Sonata/AdminBundle
     
    [MenuBundle]
        target=/bundles/Knp/Bundle/MenuBundle
     
    [KnpMenu]
        target=/knp/menu
     
    [SonataUserBundle]
        target=/bundles/Sonata/UserBundle
     
    [SonataEasyExtendsBundle]
        target=/bundles/Sonata/EasyExtendsBundle
     
    [SonataDoctrineORMAdminBundle]
        target=/bundles/Sonata/DoctrineORMAdminBundle
    to your deps file. (Don’t forget the trailing newline.)
  2. Run
    php bin/vendors install --reinstall
  3. 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',
                              ),
        // ...
    ));
  4. 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(),
                // ...
            );
            // ...
        }
  5. Add
    # app/config/config.yml
    fos_user:
        db_driver: orm
        firewall_name: main
        user_class: Application\Sonata\UserBundle\Entity\User
    to app/config/config.yml
  6. Run
    php app/console sonata:easy-extends:generate SonataUserBundle
  7. Add the new Bundle to app/AppKernel.php
    // app/AppKernel.php
        public function registerbundles()
        {
            $bundles = array(
                // Application Bundles
                // ...
                new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
                // ...
            );
            // ...
        }
  8. 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
  9. 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 }
    # ...
  10. Update your DB-Schema. Run
    php app/console doctrine:schema:update --force
  11. Now, install the assets from the different bundles:
    php app/console assets:install web
  12. Clear your cache:
    php app/console cache:clear
  13. 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
  14. You need to enable your translator in /app/config.yml.
  15. 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
Tags: , , ,

 
 




No comments:

Post a Comment