sonata block bundle - sfpot march 2014
TRANSCRIPT
BlockBundle Sonata Project
SONATA-‐PROJECT: BlockBundle
• Thomas Rabaix – Founder and Lead dev of Sonata Project – Work @ Ekino
• Official sponsor of Sonata Project • PHP Experts • Ekino[Drupal|Wordpress]Bundle, EkinoNewRelicBundle
Web Developer’s Job ~
Let’s take a standard product page hRp://demo.sonata-‐project.org/shop/product/travel-‐paris-‐
tour/1037 ~
The page point to a MVC ac[on: ProductController::viewProduct
Get the product and check validity ~
How can we reuse shared components ? Twig’s extensions: include, use, block or Symfony2’s extension: render
... or …
SONATA-‐PROJECT: BlockBundle
What ?
• BlockBundle – It is used to manage fragments of content, so-‐called blocks,
– and can be incorporated into any page layout. – Think of “small MVC ac[on”
SONATA-‐PROJECT: BlockBundle
Why ?
• Rendering controller is not always perfect – Crea[ng subrequest can be heavy – Not everything fits into an url => controller – A controller cannot define or validate segngs – Cannot handle cache backends (it is not always about hRp: redis, memcached or mongodb)
SONATA-‐PROJECT: BlockBundle
How ?
THE RSS BLOCK SERVICE Your First Block Service
SONATA-‐PROJECT: BlockBundle
Let’s create your first block service
From h3p://sonata-‐project.org/bundles/block/master/
doc/reference/your_first_block.html ~
A RSS Live Feed reader
composer require sonata-project/block-bundle:~2.2!
A block service is just a service which implements the BlockServiceInterface interface.
There is only one instance of a block service, however there are many
block instances.
1/5
A block service needs segngs to work properly and to ensure consistency,
The service should define a setDefaultSettings method to
configure the default op[ons (like a FormType)
2/5
This method must return a Response object,
this object is used to render the block.
3/5
A block template is very simple.
We are looping on feeds or if not defined, a error message is displayed.
4/5
We just need to declare the BlockService as Service with the tag sonata.tag!
We also need to enable the block
in the sonata_blocks.blocks sec[on.
5/5
CONCEPT behind the scenes
• BlockInterface!– Contains the block informa[on – Can have children and parent
• BlockContextInterface!– Contains data for the current execu[on – Can be altered during run[me
• BlockServiceInterface!– Manipulate the BlockInterface!– Render a BlockContextInterface!
• BlockContextManagerInterface!– Create a valid BlockContext from any provided data
• BlockLoaderInterface!– Call by the BlockContextManager to get a BlockInterface!
• BlockRendererInterface!– Render the BlockContext!– Generates a valid Response!
SONATA-‐PROJECT: BlockBundle
Rendering Workflow
CACHE Speed up the code
SONATA-‐PROJECT: BlockBundle
composer require sonata-project/cache-bundle:~2.1!
Cache Configura[on
Ttl default is 84600 extra_cache_keys can be used to ovewrite default
values, like ones aRached at run[me
SYMFONY2 INTEGRATION Small nice things
EXTERNAL INTEGRATIONS We are not alone
Symfony CMF
Sylius
Sonata Page Bundle
PrestaCMS
SONATA-‐PROJECT: BlockBundle
Other Features
• Block events • Javascript and Stylesheet helpers • Excep[on strategy • Rapid Prototyping
SONATA-‐PROJECT: BlockBundle
References
• hRp://sonata-‐project.org/bundles/block/master/doc/index.html
• hRp://symfony.com/doc/current/cmf/bundles/block/introduc[on.html
QUESTIONS? What the SonataBlockBundle does ?