a Symfony Product

The PHP micro-framework
based on the Symfony Components

Silex is in maintenance mode. Ends of life is set to June 2018.
Use Symfony 4 instead. Read more on Symfony's blog.
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.version_format (optional): Default format for assets.
  • assets.base_path: Default path to prepend to all assets without a package.
  • assets.base_urls: (Alternative to assets.base_path) List of base URLs to choose from to prepend to 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).
  • assets.json_manifest_path (optional): Absolute path to a JSON version manifest.

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 using the asset() method. It takes two arguments. In the case of named packages, the first is the path relative to the base_path specified in the package definition and the second is the package name. For unmamed packages, there is only one argument, the path relative to the assets folder:

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-2018 Symfony