a SensioLabs Product

The PHP micro-framework
based on the Symfony Components

You are reading the documentation for Silex 1.3. Switch to the documentation for Silex 2.0.

Questions & Feedback

License

Creative Commons License Silex documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

HTTP Fragment

The HttpFragmentServiceProvider provides support for the Symfony fragment sub-framework, which allows you to embed fragments of HTML in a template.

Warning

This service provider only work with Symfony 2.4+.

Parameters

  • fragment.path: The path to use for the URL generated for ESI and HInclude URLs (/_fragment by default).
  • uri_signer.secret: The secret to use for the URI signer service (used for the HInclude renderer).
  • fragment.renderers.hinclude.global_template: The content or Twig template to use for the default content when using the HInclude renderer.

Services

  • fragment.handler: An instance of FragmentHandler.
  • fragment.renderers: An array of fragment renderers (by default, the inline, ESI, and HInclude renderers are pre-configured).

Registering

1
$app->register(new Silex\Provider\HttpFragmentServiceProvider());

Usage

Note

This section assumes that you are using Twig for your templates.

Instead of building a page out of a single request/controller/template, the fragment framework allows you to build a page from several controllers/sub-requests/sub-templates by using fragments.

Including "sub-pages" in the main page can be done with the Twig render() function:

1
2
3
4
5
The main page content.

{{ render('/foo') }}

The main page content resumes here.

The render() call is replaced by the content of the /foo URL (internally, a sub-request is handled by Silex to render the sub-page).

Instead of making internal sub-requests, you can also use the ESI (the sub-request is handled by a reverse proxy) or the HInclude strategies (the sub-request is handled by a web browser):

1
2
3
4
5
{{ render(url('route_name')) }}

{{ render_esi(url('route_name')) }}

{{ render_hinclude(url('route_name')) }}
Website powered by Symfony and Twig, deployed on
The Silex logo is © 2010-2017 SensioLabs