The SonataBlockBundle allows developers to manage reusable content fragments called blocks that can be incorporated into page layouts. It provides an interface and services to define block content through block services, handle block rendering, and integrate blocks with caching for improved performance. The bundle is used by several Symfony projects and e-commerce platforms to build modular and reusable content.
2.
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
3.
4.
5. 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
…
6.
7.
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”
8.
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)
11.
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
13. 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
14. 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
15. This
method
must
return
a
Response
object,
this
object
is
used
to
render
the
block.
3/5
16. A
block
template
is
very
simple.
We
are
looping
on
feeds
or
if
not
defined,
a
error
message
is
displayed.
4/5
17. 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
20. • 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!