@@ -26,15 +26,16 @@ simple. Hardcoding URLs can be a disadvantage because:
2626 asset. When using the Asset component, you can group assets in packages to
2727 avoid repeating the common part of their path;
2828* **Versioning is difficult **: it has to be custom managed for each
29- application. Adding a version to the asset URLs is essential for some applications
30- because it allows to control how the assets are cached. The Asset component
31- allows to define different versioning strategies for each package;
29+ application. Adding a version (e.g. ``main.css?v=5 ``) to the asset URLs
30+ is essential for some applications because it allows you to control how
31+ the assets are cached. The Asset component allows you to define different
32+ versioning strategies for each package;
3233* **Moving assets location ** is cumbersome and error-prone: it requires you to
3334 carefully update the URLs of all assets included in all templates. The Asset
3435 component allows to move assets effortlessly just by changing the base path
3536 value associated with the package of assets;
36- * **It's nearly impossible to use multiple CDNs **: this technique requires to
37- change the URL of the asset randomly for each request. The Asset component
37+ * **It's nearly impossible to use multiple CDNs **: this technique requires
38+ you to change the URL of the asset randomly for each request. The Asset component
3839 provides out-of-the-box support for any number of multiple CDNs, both regular
3940 (``http:// ``) and secure (``https:// ``).
4041
@@ -65,7 +66,7 @@ any versioning::
6566 echo $package->getUrl('/image.png');
6667 // result: /image.png
6768
68- Packages implement the :class: `Symfony\\ Component\\ Asset\\ PackageInterface `,
69+ Packages implement :class: `Symfony\\ Component\\ Asset\\ PackageInterface `,
6970which defines the following two methods:
7071
7172:method: `Symfony\\ Component\\ Asset\\ PackageInterface::getVersion `
@@ -74,18 +75,27 @@ which defines the following two methods:
7475:method: `Symfony\\ Component\\ Asset\\ PackageInterface::getUrl `
7576 Returns an absolute or root-relative public path.
7677
78+ With a package, you can:
79+
80+ A) :ref: `version the assets <component-assets-versioning >`;
81+ B) set a :ref: `common base path (e.g. /css) <component-assets-path-package >`
82+ for the assets;
83+ C) :ref: `configure a CDN <component-assets-cdn >` for the assets
84+
85+ .. _component-assets-versioning :
86+
7787Versioned Assets
7888~~~~~~~~~~~~~~~~
7989
80- One of the main features of the Asset component is to manage the versioning of
81- the application's assets. Asset versions are commonly used to control how these
82- assets are cached.
90+ One of the main features of the Asset component is the ability to manage
91+ the versioning of the application's assets. Asset versions are commonly used
92+ to control how these assets are cached.
8393
84- Instead of relying on a simple version mechanism, the Asset component allows to
85- define advanced versioning strategies via PHP classes. The two built-in strategies
86- provided by the component are :class: `Symfony\\ Component\\ Asset\\ VersionStrategy\\ EmptyVersionStrategy `,
94+ Instead of relying on a simple version mechanism, the Asset component allows
95+ you to define advanced versioning strategies via PHP classes. The two built-in
96+ strategies are the :class: `Symfony\\ Component\\ Asset\\ VersionStrategy\\ EmptyVersionStrategy `,
8797which doesn't add any version to the asset and :class: `Symfony\\ Component\\ Asset\\ VersionStrategy\\ StaticVersionStrategy `,
88- which allows to set the version with a format string.
98+ which allows you to set the version with a format string.
8999
90100In this example, the ``StaticVersionStrategy `` is used to append the ``v1 ``
91101suffix to any asset path::
@@ -143,13 +153,15 @@ every day::
143153 }
144154 }
145155
156+ .. _component-assets-path-package :
157+
146158Grouped Assets
147159~~~~~~~~~~~~~~
148160
149- It's common for applications to store their assets in a common path. If that's
150- your case, replace the default :class: `Symfony\\ Component\\ Asset\\ Package ` class
151- by :class: `Symfony\\ Component\\ Asset\\ PathPackage ` to avoid repeating the same
152- path time and again::
161+ Often, many assets live under a common path (e.g. `` /static/images ``). If
162+ that's your case, replace the default :class: `Symfony\\ Component\\ Asset\\ Package `
163+ class with :class: `Symfony\\ Component\\ Asset\\ PathPackage ` to avoid repeating
164+ that path time and again::
153165
154166 use Symfony\Component\Asset\PathPackage;
155167 // ...
@@ -162,9 +174,9 @@ path time and again::
162174Request Context Aware Assets
163175............................
164176
165- If you are also using the HttpFoundation component in your project, for example
166- in a Symfony application, the ``PathPackage `` class can take into account the
167- context of the current request::
177+ If you are also using the :doc: ` HttpFoundation</components/http_foundation/introduction> `
178+ component in your project (for example in a Symfony application) , the ``PathPackage ``
179+ class can take into account the context of the current request::
168180
169181 use Symfony\Component\Asset\PathPackage;
170182 use Symfony\Component\Asset\Context\RequestStackContext;
@@ -180,9 +192,12 @@ context of the current request::
180192 // result: /somewhere/static/images/logo.png?v1
181193
182194When the request context is set (via an optional third argument), in addition to
183- the configured base path, ``PathPackage `` also prepends the current request base
184- URL (``/somewhere/ `` in this example) to assets. This allows your website to be
185- hosted anywhere under the web server root directory.
195+ the configured base path (i.e. ``/static/images ``), ``PathPackage `` also
196+ prepends the current request base URL. So, for example, if your entire site
197+ is hosted under the ``/somewhere `` directory in your web server root directory,
198+ then ``/somewhere/ `` will be prefixed to all paths.
199+
200+ .. _component-assets-cdn :
186201
187202Absolute Assets and CDNs
188203~~~~~~~~~~~~~~~~~~~~~~~~
@@ -202,25 +217,44 @@ class to generate absolute URLs for their assets::
202217 echo $package->getUrl('/logo.png');
203218 // result: http://static.example.com/images/logo.png?v1
204219
220+ You can also pass a schema-agnostic URL::
221+
222+ use Symfony\Component\Asset\UrlPackage;
223+ // ...
224+
225+ $package = new UrlPackage(
226+ '//static.example.com/images/',
227+ new StaticVersionStrategy('v1')
228+ );
229+
230+ echo $package->getUrl('/logo.png');
231+ // result: //static.example.com/images/logo.png?v1
232+
233+ This is useful because assets will automatically be requested via https if
234+ a visitor is viewing your site in https. Just make sure that your CDN host
235+ supports https.
236+
205237In case you serve assets from more than one domain to improve application
206- performance, pass an array of URLs as the first argument of ``UrlPackage ``
238+ performance, pass an array of URLs as the first argument to the ``UrlPackage ``
207239constructor::
208240
209241 use Symfony\Component\Asset\UrlPackage;
210242 // ...
211243
212244 $urls = array(
213- 'http: //static1.example.com/images/',
214- 'http: //static2.example.com/images/',
245+ '//static1.example.com/images/',
246+ '//static2.example.com/images/',
215247 );
216248 $package = new UrlPackage($urls, new StaticVersionStrategy('v1'));
217249
218250 echo $package->getUrl('/logo.png');
219251 // result: http://static1.example.com/images/logo.png?v1
252+ echo $package->getUrl('/icon.png');
253+ // result: http://static2.example.com/images/icon.png?v1
220254
221- The selection of the domain which will serve the asset is deterministic, meaning
222- that each asset will be always served by the same domain. This behavior simplifies
223- the management of HTTP cache.
255+ For each asset, one of the URLs will be randomly used. But, the selection
256+ is deterministic, meaning that each asset will be always served by the same
257+ domain. This behavior simplifies the management of HTTP cache.
224258
225259Request Context Aware Assets
226260............................
@@ -240,6 +274,7 @@ protocol-relative URLs for HTTPs requests, any base URL for HTTP requests)::
240274 new RequestStackContext($requestStack)
241275 );
242276
277+ // assuming the RequestStackContext says that we are on a secure host
243278 echo $package->getUrl('/logo.png');
244279 // result: https://example.com/logo.png?v1
245280
0 commit comments