a SensioLabs Product

The PHP micro-framework
based on the Symfony Components

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

Questions & Feedback

License

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

Asset

The AssetServiceProvider provides a way to manage URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files.

Parameters

  • assets.version: Default version for assets.
  • assets.format_version (optional): Default format for assets.
  • assets.base_path: Default path to prepend to all assets without a package.
  • assets.named_packages (optional): Named packages. Keys are the package names and values the configuration (supported keys are version, version_format, base_urls, and base_path).

Services

  • assets.packages: The asset service.

Registering

1
2
3
4
5
6
7
8
$app->register(new Silex\Provider\AssetServiceProvider(), array(
    'assets.version' => 'v1',
    'assets.version_format' => '%s?version=%s',
    'assets.named_packages' => array(
        'css' => array('version' => 'css2', 'base_path' => '/whatever-makes-sense'),
        'images' => array('base_urls' => array('https://img.example.com')),
    ),
));

Note

Add the Symfony Asset Component as a dependency:

1
composer require symfony/asset

If you want to use assets in your Twig templates, you must also install the Symfony Twig Bridge:

1
composer require symfony/twig-bridge

Usage

The AssetServiceProvider is mostly useful with the Twig provider:

1
2
3
4
5
{{ asset('/css/foo.png') }}
{{ asset('/css/foo.css', 'css') }}
{{ asset('/img/foo.png', 'images') }}

{{ asset_version('/css/foo.png') }}

For more information, check out the Asset Component documentation.

Website powered by Symfony and Twig, deployed on
The Silex logo is © 2010-2017 SensioLabs