sonata block bundle - sfpot march 2014

36
BlockBundle Sonata Project

Upload: th0masr

Post on 04-Jun-2015

4.407 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Sonata Block Bundle - sfPot March 2014

BlockBundle  Sonata  Project  

Page 2: Sonata Block Bundle - sfPot March 2014

               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    

Page 3: Sonata Block Bundle - sfPot March 2014
Page 4: Sonata Block Bundle - sfPot March 2014
Page 5: Sonata Block Bundle - sfPot March 2014

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  …  

 

Page 6: Sonata Block Bundle - sfPot March 2014
Page 7: Sonata Block Bundle - sfPot March 2014

               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”  

Page 8: Sonata Block Bundle - sfPot March 2014

               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)  

Page 9: Sonata Block Bundle - sfPot March 2014

               SONATA-­‐PROJECT:  BlockBundle  

How  ?  

Page 10: Sonata Block Bundle - sfPot March 2014

THE  RSS  BLOCK  SERVICE  Your  First  Block  Service  

Page 11: Sonata Block Bundle - sfPot March 2014

               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  

Page 12: Sonata Block Bundle - sfPot March 2014

composer require sonata-project/block-bundle:~2.2!

Page 13: Sonata Block Bundle - sfPot March 2014

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  

Page 14: Sonata Block Bundle - sfPot March 2014

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  

Page 15: Sonata Block Bundle - sfPot March 2014

This  method  must  return  a  Response  object,      

this  object  is  used  to  render  the  block.  

3/5  

Page 16: Sonata Block Bundle - sfPot March 2014

A  block  template  is  very  simple.      

We  are  looping  on  feeds  or  if  not  defined,  a  error  message  is  displayed.  

4/5  

Page 17: Sonata Block Bundle - sfPot March 2014

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  

Page 18: Sonata Block Bundle - sfPot March 2014
Page 19: Sonata Block Bundle - sfPot March 2014

CONCEPT  behind  the  scenes  

Page 20: Sonata Block Bundle - sfPot March 2014

•  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!

Page 21: Sonata Block Bundle - sfPot March 2014

               SONATA-­‐PROJECT:  BlockBundle  

Rendering  Workflow  

Page 22: Sonata Block Bundle - sfPot March 2014

CACHE  Speed  up  the  code  

Page 23: Sonata Block Bundle - sfPot March 2014

               SONATA-­‐PROJECT:  BlockBundle  

composer require sonata-project/cache-bundle:~2.1!

Page 24: Sonata Block Bundle - sfPot March 2014

Cache  Configura[on  

Page 25: Sonata Block Bundle - sfPot March 2014

Ttl  default  is  84600  extra_cache_keys  can  be  used  to  ovewrite  default  

values,  like  ones  aRached  at  run[me  

Page 26: Sonata Block Bundle - sfPot March 2014

SYMFONY2  INTEGRATION  Small  nice  things  

Page 27: Sonata Block Bundle - sfPot March 2014
Page 28: Sonata Block Bundle - sfPot March 2014
Page 29: Sonata Block Bundle - sfPot March 2014

EXTERNAL  INTEGRATIONS  We  are  not  alone  

Page 30: Sonata Block Bundle - sfPot March 2014

Symfony  CMF  

Page 31: Sonata Block Bundle - sfPot March 2014

Sylius  

Page 32: Sonata Block Bundle - sfPot March 2014

Sonata  Page  Bundle  

Page 33: Sonata Block Bundle - sfPot March 2014

PrestaCMS  

Page 34: Sonata Block Bundle - sfPot March 2014

               SONATA-­‐PROJECT:  BlockBundle  

Other  Features  

•  Block  events  •  Javascript  and  Stylesheet  helpers  •  Excep[on  strategy  •  Rapid  Prototyping  

Page 35: Sonata Block Bundle - sfPot March 2014

               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  

Page 36: Sonata Block Bundle - sfPot March 2014

QUESTIONS?  What  the  SonataBlockBundle  does  ?