Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Components
  4. Templating
  5. Assets Helper
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Configure Paths
  • Absolute Urls
  • Versioning
  • Multiple Packages
  • Custom Packages

Assets Helper

Edit this page

Warning: You are browsing the documentation for Symfony 2.8, which is no longer maintained.

Consider upgrading your projects to Symfony 6.2.

Assets Helper

The assets helper's main purpose is to make your application more portable by generating asset paths:

1
2
3
<link href="<?php echo $view['assets']->getUrl('css/style.css') ?>" rel="stylesheet">

<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>">

The assets helper can then be configured to render paths to a CDN or modify the paths in case your assets live in a sub-directory of your host (e.g. http://example.com/app).

Configure Paths

By default, the assets helper will prefix all paths with a slash. You can configure this by passing a base assets path as the first argument of the constructor:

1
2
3
4
use Symfony\Component\Templating\Helper\AssetsHelper;

// ...
$templateEngine->set(new AssetsHelper('/foo/bar'));

Now, if you use the helper, everything will be prefixed with /foo/bar:

1
2
3
4
<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>">
<!-- renders as:
<img src="/foo/bar/images/logo.png">
-->

Absolute Urls

You can also specify a URL to use in the second parameter of the constructor:

1
2
// ...
$templateEngine->set(new AssetsHelper(null, 'http://cdn.example.com/'));

Now URLs are rendered like http://cdn.example.com/images/logo.png.

You can also use the third argument of the helper to force an absolute URL:

1
2
3
4
<img src="<?php echo $view['assets']->getUrl('images/logo.png', null, true) ?>">
<!-- renders as:
<img src="http://yourwebsite.com/foo/bar/images/logo.png">
-->

Note

If you already set a URL in the constructor, using the third argument of getUrl() will not affect the generated URL.

Versioning

To avoid using the cached resource after updating the old resource, you can use versions which you bump every time you release a new project. The version can be specified in the third argument:

1
2
// ...
$templateEngine->set(new AssetsHelper(null, null, '328rad75'));

Now, every URL is suffixed with ?328rad75. If you want to have a different format, you can specify the new format in fourth argument. It's a string that is used in sprintf. The first argument is the path and the second is the version. For instance, %s?v=%s will be rendered as /images/logo.png?v=328rad75.

You can also generate a versioned URL on an asset-by-asset basis using the fourth argument of the helper:

1
2
3
4
<img src="<?php echo $view['assets']->getUrl('images/logo.png', null, false, '3.0') ?>">
<!-- renders as:
<img src="/images/logo.png?v=3.0">
-->

Multiple Packages

Asset path generation is handled internally by packages. The component provides 2 packages by default:

  • PathPackage
  • UrlPackage

You can also use multiple packages:

1
2
3
4
5
6
7
use Symfony\Component\Templating\Asset\PathPackage;

// ...
$templateEngine->set(new AssetsHelper());

$templateEngine->get('assets')->addPackage('images', new PathPackage('/images/'));
$templateEngine->get('assets')->addPackage('scripts', new PathPackage('/scripts/'));

This will setup the assets helper with 3 packages: the default package which defaults to / (set by the constructor), the images package which prefixes it with /images/ and the scripts package which prefixes it with /scripts/.

If you want to set another default package, you can use setDefaultPackage().

You can specify which package you want to use in the second argument of getUrl():

1
2
3
4
<img src="<?php echo $view['assets']->getUrl('foo.png', 'images') ?>">
<!-- renders as:
<img src="/images/foo.png">
-->

Custom Packages

You can create your own package by extending Package.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Barney Hanlon, a Symfony contributor

    Thanks Barney Hanlon for being a Symfony contributor

    2 commits • 40 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia