Install Sonata

Install Sonata

Note

To speed up the installation, all composer are used with –no update option. composer update is only used at the end of the installation to get all bundles in once.

Get Sonata

Make sure composer is updated

# sudo /usr/local/bin/composer self-update

Get Sonata Bundle

# composer require sonata-project/admin-bundle --no-update

Get ORM Bundle

# composer require sonata-project/doctrine-orm-admin-bundle --no-update

Get SonataBlockBundle

# composer require sonata-project/block-bundle --no-update

Get KnpMenuBundle

# composer require knplabs/knp-menu-bundle --no-update

Install Bundles

Now you can install bundles with composer:

# composer update

KnpMenuBundle config

Add in app/config/config.yml

# app/config/config.yml
knp_menu:
    twig:  # use "twig: false" to disable the Twig extension and the TwigRenderer
        template: knp_menu.html.twig
    templating: false # if true, enables the helper for PHP templates
    default_renderer: twig # The renderer to use, list is also available by default

Enable Admin

Add in app/config/config.yml:

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

Don’t forget to enable SonataAdminBundle too:

<?php
// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        // Add Sonata dependencies
        new Sonata\CoreBundle\SonataCoreBundle(),
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Knp\Bundle\MenuBundle\KnpMenuBundle(),
        // Uses SonataDoctrineORMAdmin
        new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
        // Then add SonataAdminBundle
        new Sonata\AdminBundle\SonataAdminBundle(),
        // ...
    );
}

Install the asset from bundle

# php app/console assets:install web

Delete the cache

# php app/console cache:clear

Create Admin File

in /src/Acme/AdminBundle/, create Admin directory

in /src/Acme/AdminBundle/Admin, create PostAdmin.php, with:

<?php
namespace Acme\AdminBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;

/**
 * Post
 */
class PostAdmin extends Admin
{
}

Install Sonata admin

add its routes to your application’s routing file

# app/config/routing.yml
admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

Create either a new admin.xml file inside the Acme/AdminBundle/Resources/config/ folder:

<!-- Acme/AdminBundle/Resources/config/admin.xml -->
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
       <service id="sonata.admin.post" class="Acme\AdminBundle\Admin\PostAdmin">
          <tag name="sonata.admin" manager_type="orm" group="Content" label="Post"/>
          <argument />
          <argument>Acme\AdminBundle\Entity\Post</argument>
          <argument />
          <call method="setTranslationDomain">
              <argument>AcmeAdminBundle</argument>
          </call>
      </service>
   </services>
</container>

Include your new configuration file in the main config.yml

# app/config/config.yml
imports:
    - { resource: @AcmeAdminBundle/Resources/config/admin.xml }

Install Easyextends

# composer require sonata-project/easy-extends-bundle --no-update

Composer update

# composer update

Enable EasyExtendsBundle

Add

<?php
// app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
        // ...
    );
}

Enable search

Enable simple search in sonata admin is easy, just add in app/config/config.yml:

sonata_admin:                                                                                  
     templates:                                                                  
         search:              SonataAdminBundle:Core:search.html.twig            
         search_result_block: SonataAdminBundle:Block:block_search_result.html.twig
sonata_block:                                                                   
      default_contexts: [cms]                                                     
      blocks:                                                                     
          sonata.admin.block.search_result:                                       
              contexts: [admin]

But it may be difficult to use if you have a lot data in your Database

Raphaël has written 45 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>