@@ -6,7 +6,37 @@ The Asset Component
6
6
===================
7
7
8
8
The Asset component manages URL generation and versioning of web assets such
9
- as CSS stylsheets, JavaScript files and image files.
9
+ as CSS stylesheets, JavaScript files and image files.
10
+
11
+ In the past, it was common for web applications to hardcode the URLs of the web
12
+ assets. For example:
13
+
14
+ .. code-block :: html
15
+
16
+ <link rel =" stylesheet" type =" text/css" href =" /css/main.css" >
17
+
18
+ <!-- ... -->
19
+
20
+ <a href =" /" ><img src =" /images/logo.png" ></a >
21
+
22
+ This practice is no longer recommended unless the web application is extremely
23
+ simple. The main problems of hardcoding asset URLs are the following:
24
+
25
+ * **Templates get verbose ** because you have to write the full path for each
26
+ asset. When using the Asset component, you can group assets in packages to
27
+ avoid repeating the common part of their path.
28
+ * **Versioning is difficult ** because 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.
32
+ * **Moving assets location ** is cumbersome and error-prone, because it requires
33
+ you to carefully update the URLs of all assets included in all templates.
34
+ The Asset component allows to move assets effortlessly just by changing the
35
+ base path value associated with the package of assets.
36
+ * **Impossible to use multiple CDNs ** because it requires to change the URL of
37
+ the asset randomly for each request. The Asset component provides out-of-the-box
38
+ support for any number of multiple CDNs, both regular (``http:// ``) and
39
+ secure (``https:// ``).
10
40
11
41
Installation
12
42
------------
22
52
Asset Packages
23
53
~~~~~~~~~~~~~~
24
54
25
- The Asset component manages its assets through packages. A package groups all
26
- the assets which use the same versioning strategy. In the following basic
27
- example, a package is created to manage assets without any versioning::
55
+ The Asset component manages assets through packages. A package groups all the
56
+ assets which share the same properties: versioning strategy, base path, CDN hosts,
57
+ etc. In the following basic example, a package is created to manage assets without
58
+ any versioning::
28
59
29
60
use Symfony\Component\Asset\Package;
30
61
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
@@ -139,16 +170,19 @@ can take into account the context of the current request::
139
170
use Symfony\Component\Asset\Context\RequestStackContext;
140
171
// ...
141
172
142
- $package = new PathPackage('/static/images', new StaticVersionStrategy('v1'));
143
- $package->setContext(new RequestStackContext($requestStack));
173
+ $package = new PathPackage(
174
+ '/static/images',
175
+ new StaticVersionStrategy('v1'),
176
+ new RequestStackContext($requestStack)
177
+ );
144
178
145
179
echo $package->getUrl('/logo.png');
146
180
// result: /somewhere/static/images/logo.png?v1
147
181
148
- When the request context is set, in addition to the configured base path,
149
- :class: `Symfony\C omponent\A sset\P athPackage ` also prepends the current request
150
- base URL (``/somewhere/ `` in this example) to assets. This allows your website
151
- to be hosted anywhere under the web server root directory.
182
+ When the request context is set (via the third optional argument), in addition
183
+ to the configured base path, :class: `Symfony\C omponent\A sset\P athPackage ` also
184
+ prepends the current request base URL (``/somewhere/ `` in this example) to assets.
185
+ This allows your website to be hosted anywhere under the web server root directory.
152
186
153
187
Absolute Assets and CDNs
154
188
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -202,9 +236,9 @@ protocol-relative URLs for HTTPs requests, any base URL for HTTP requests)::
202
236
203
237
$package = new UrlPackage(
204
238
array('http://example.com/', 'https://example.com/'),
205
- new StaticVersionStrategy('v1')
239
+ new StaticVersionStrategy('v1'),
240
+ new RequestStackContext($requestStack)
206
241
);
207
- $package->setContext(new RequestStackContext($requestStack));
208
242
209
243
echo $package->getUrl('/logo.png');
210
244
// result: https://example.com/logo.png?v1
@@ -237,11 +271,11 @@ they all have different base paths::
237
271
238
272
$packages = new Packages($defaultPackage, $namedPackages)
239
273
240
- The :class: `Symfony\C omponent\A sset\P ackages ` class requires to define a default
241
- package which will be applied to all assets except those which indicate the name
242
- of the package to use. In addition, this application defines a package named
243
- `` img `` to serve images from an external domain and a ``doc `` package to avoid
244
- repeating long paths when linking to a document inside a template::
274
+ The :class: `Symfony\C omponent\A sset\P ackages ` class allows to define a default
275
+ package, which will be applied to assets that don't define the name of package
276
+ to use. In addition, this application defines a package named `` img `` to serve
277
+ images from an external domain and a ``doc `` package to avoid repeating long
278
+ paths when linking to a document inside a template::
245
279
246
280
echo $packages->getUrl('/main.css');
247
281
// result: /main.css?v1
0 commit comments