15 januari 2016

Beginning with Sonata Project

Sonata Project is a bundle collection for Symfony to basically make your day-to-day CRUD programming tasks easier. Hooking up your precious doctrine orm models with a backend interface has never been easier!

So let's get started! We are going to build a portfolio system with tags and clients ORM attached to it. First we set up our Symfony project from scratch by running composer:

composer create-projects symfony/framework-standard-edition my_portfolio "2.8.*"

If your setup asks a password for mysql, use root for user and root for password. That way, you can quickly start in Vagrant using your application.

Your project will look like this:

blog-Beginning-with-Sonata-Project-project

You can use xamp or any lamp stack on your local pc, but Vagrant is a better solution for this. A simple Vagrant file setup like this can be used to quickly get a lamp stack.

Next step is adding the minimum required Sonata bundles: 

composer require knplabs/knp-menu-bundle dev-master --no-update
composer require sonata-project/admin-bundle dev-master --no-update 
composer require sonata-project/block-bundle dev-master --no-update 
composer require sonata-project/core-bundle dev-master --no-update 
composer require sonata-project/datagrid-bundle dev-master --no-update 
composer require sonata-project/doctrine-orm-admin-bundle dev-master --no-update 
composer require sonata-project/easy-extends-bundle dev-master --no-update 
composer update

The vendor map is now updated and you can add the bundles to the appkernel. Add the following lines:

Next step is to configure the application for Sonata with the minimum configuration.

Start by creating this structure in your app folder:

blog-Beginning-with-Sonata-Project-structure

In your config.yml you adjust the imports lines as follows:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: sonata/block.yml }
    - { resource: sonata/admin.yml }

We also need to enable the translator of Symfony which is turned off by default in the configuration, uncomment and make the configuration look like this:

parameters:
    locale: en

framework:
    #esi:             ~
    translator:      { fallbacks: ["%locale%"] }

Then we add the following configuration to sonata/block.yml:

sonata_block:
    default_contexts: [sonata_page_bundle]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]

Next, we need to create the admin route. Add the following line to your app/config/routing.yml:

admin_area:
    resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
    prefix: /admin
    
_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

Sonata needs assets for the admin, so run the following commands:

php app/console cache:clear
php app/console assets:install
Congratulations, you are now set and done. Sonata is installed with the least options!
 

You can now surf to http://dev.myportfolio.com/admin and you will go to the Sonata dashboard without authentication.

blog-Beginning-with-Sonata-Project-sonata

In our next blogpost about Sonata we are going to configure the orm admin in depth. So stay tuned!

Samenwerken?

Op zoek naar innovatieve manieren om te groeien?

Neem contact op