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, andbase_path). - assets.json_manifest_path (optional): Absolute path to a JSON version manifest.
Services¶
- assets.packages: The asset service.
Registering¶
$app->register(new Mascot\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:
composer require symfony/asset
If you want to use assets in your Twig templates, you must also install the Symfony Twig Bridge:
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:
{{ 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.