@@ -26,15 +26,16 @@ simple. Hardcoding URLs can be a disadvantage because:
26
26
asset. When using the Asset component, you can group assets in packages to
27
27
avoid repeating the common part of their path;
28
28
* **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;
32
33
* **Moving assets location ** is cumbersome and error-prone: it requires you to
33
34
carefully update the URLs of all assets included in all templates. The Asset
34
35
component allows to move assets effortlessly just by changing the base path
35
36
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
38
39
provides out-of-the-box support for any number of multiple CDNs, both regular
39
40
(``http:// ``) and secure (``https:// ``).
40
41
@@ -65,7 +66,7 @@ any versioning::
65
66
echo $package->getUrl('/image.png');
66
67
// result: /image.png
67
68
68
- Packages implement the :class: `Symfony\\ Component\\ Asset\\ PackageInterface `,
69
+ Packages implement :class: `Symfony\\ Component\\ Asset\\ PackageInterface `,
69
70
which defines the following two methods:
70
71
71
72
:method: `Symfony\\ Component\\ Asset\\ PackageInterface::getVersion `
@@ -74,18 +75,27 @@ which defines the following two methods:
74
75
:method: `Symfony\\ Component\\ Asset\\ PackageInterface::getUrl `
75
76
Returns an absolute or root-relative public path.
76
77
78
+ With a package, you can:
79
+
80
+ A) :ref: `version the assets <component-assets-versioning >`;
81
+ B) set a :ref: `common base path <component-assets-path-package >` (e.g. ``/css ``)
82
+ for the assets;
83
+ C) :ref: `configure a CDN <component-assets-cdn >` for the assets
84
+
85
+ .. _component-assets-versioning :
86
+
77
87
Versioned Assets
78
88
~~~~~~~~~~~~~~~~
79
89
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.
83
93
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 `,
87
97
which 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.
89
99
90
100
In this example, the ``StaticVersionStrategy `` is used to append the ``v1 ``
91
101
suffix to any asset path::
@@ -143,13 +153,15 @@ every day::
143
153
}
144
154
}
145
155
156
+ .. _component-assets-path-package :
157
+
146
158
Grouped Assets
147
159
~~~~~~~~~~~~~~
148
160
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 over and over again::
153
165
154
166
use Symfony\Component\Asset\PathPackage;
155
167
// ...
@@ -162,9 +174,9 @@ path time and again::
162
174
Request Context Aware Assets
163
175
............................
164
176
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 instance, in a Symfony application) , the ``PathPackage ``
179
+ class can take into account the context of the current request::
168
180
169
181
use Symfony\Component\Asset\PathPackage;
170
182
use Symfony\Component\Asset\Context\RequestStackContext;
@@ -179,10 +191,13 @@ context of the current request::
179
191
echo $package->getUrl('/logo.png');
180
192
// result: /somewhere/static/images/logo.png?v1
181
193
182
- When 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.
194
+ Now that the request context is set, the ``PathPackage `` will prepend the
195
+ current request base URL. So, for example, if your entire site is hosted under
196
+ the ``/somewhere `` directory of your web server root directory and the configured
197
+ base path is ``/static/images ``, all paths will be prefixed with
198
+ ``/somewhere/static/images ``.
199
+
200
+ .. _component-assets-cdn :
186
201
187
202
Absolute Assets and CDNs
188
203
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -202,25 +217,44 @@ class to generate absolute URLs for their assets::
202
217
echo $package->getUrl('/logo.png');
203
218
// result: http://static.example.com/images/logo.png?v1
204
219
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
+
205
237
In 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 ``
207
239
constructor::
208
240
209
241
use Symfony\Component\Asset\UrlPackage;
210
242
// ...
211
243
212
244
$urls = array(
213
- 'http: //static1.example.com/images/',
214
- 'http: //static2.example.com/images/',
245
+ '//static1.example.com/images/',
246
+ '//static2.example.com/images/',
215
247
);
216
248
$package = new UrlPackage($urls, new StaticVersionStrategy('v1'));
217
249
218
250
echo $package->getUrl('/logo.png');
219
251
// 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
220
254
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.
224
258
225
259
Request Context Aware Assets
226
260
............................
@@ -241,6 +275,7 @@ protocol-relative URLs for HTTPs requests, any base URL for HTTP requests)::
241
275
);
242
276
243
277
echo $package->getUrl('/logo.png');
278
+ // assuming the RequestStackContext says that we are on a secure host
244
279
// result: https://example.com/logo.png?v1
245
280
246
281
Named Packages
0 commit comments