@@ -24,9 +24,7 @@ Asset Packages
24
24
25
25
The Asset component manages its assets through packages. A package groups all
26
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:
28
-
29
- .. code-block :: php
27
+ example, a package is created to manage assets without any versioning::
30
28
31
29
use Symfony\Component\Asset\Package;
32
30
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
@@ -36,23 +34,14 @@ example, a package is created to manage assets without any versioning:
36
34
echo $package->getUrl('/image.png');
37
35
// result: /image.png
38
36
39
- Packages implement the `` PackageInterface ``, which defines the following two
40
- methods::
37
+ Packages implement the :class: ` Symfony \\ Component \\ Asset \\ PackageInterface `,
38
+ which defines the following two methods::
41
39
42
- namespace Symfony\Component\Asset;
40
+ :method: `Symfony\\ Component\\ Asset\\ PackageInterface::getVersion `
41
+ Returns the asset version for an asset.
43
42
44
- interface PackageInterface
45
- {
46
- /**
47
- * Returns the asset version for an asset.
48
- */
49
- public function getVersion($path);
50
-
51
- /**
52
- * Returns an absolute or root-relative public path.
53
- */
54
- public function getUrl($path);
55
- }
43
+ :method: `Symfony\\ Component\\ Asset\\ PackageInterface::getUrl `
44
+ Returns an absolute or root-relative public path.
56
45
57
46
Versioned Assets
58
47
~~~~~~~~~~~~~~~~
@@ -63,12 +52,12 @@ assets are cached.
63
52
64
53
Instead of relying on a simple version mechanism, the Asset component allows to
65
54
define advanced versioning strategies via PHP classes. The two built-in strategies
66
- provided by the component are `` EmptyVersionStrategy ``, which doesn't add any
67
- version to the asset, and `` StaticVersionStrategy ``, which allows to set the
68
- version with a format string.
55
+ provided by the component are :class: ` Symfony \C omponent \A sset \V ersionStrategy \ E mptyVersionStrategy `,
56
+ which doesn't add any version to the asset and :class: ` Symfony \C omponent \A sset \V ersionStrategy \ S taticVersionStrategy `,
57
+ which allows to set the version with a format string.
69
58
70
- In this example, the `` StaticVersionStrategy `` is used to append the `` v1 ` `
71
- suffix to any asset path::
59
+ In this example, the :class: ` Symfony \C omponent \A sset \V ersionStrategy \ S taticVersionStrategy `
60
+ is used to append the `` v1 `` suffix to any asset path::
72
61
73
62
use Symfony\Component\Asset\Package;
74
63
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
@@ -79,7 +68,8 @@ suffix to any asset path::
79
68
// result: /image.png?v1
80
69
81
70
In case you want to modify the version format, pass a sprintf-compatible format
82
- string as the second argument of the ``StaticVersionStrategy `` constructor::
71
+ string as the second argument of the
72
+ :class: `Symfony\C omponent\A sset\V ersionStrategy\S taticVersionStrategy ` constructor::
83
73
84
74
// put the 'version' word before the version value
85
75
$package = new Package(new StaticVersionStrategy('v1', '%s?version=%s'));
@@ -96,9 +86,9 @@ string as the second argument of the ``StaticVersionStrategy`` constructor::
96
86
Custom Version Strategies
97
87
.........................
98
88
99
- Use the `` VersionStrategyInterface `` to define your own version strategy. For
100
- example, you could define a versioning where the current date is appended to
101
- bust the cache every day::
89
+ Use the :class: ` Symfony \C omponent \A sset \V ersionStrategy \ V ersionStrategyInterface `
90
+ to define your own version strategy. For example, you could define a versioning
91
+ where the current date is appended to bust the cache every day::
102
92
103
93
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
104
94
@@ -126,10 +116,12 @@ Grouped Assets
126
116
~~~~~~~~~~~~~~
127
117
128
118
It's common for applications to store their assets in a common path. If that's
129
- your case, replace the default ``Package `` class by ``PathPackage `` to avoid
130
- repeating the same path time and again::
119
+ your case, replace the default :class: `Symfony\C omponent\A sset\P ackage ` class by
120
+ :class: `Symfony\C omponent\A sset\P athPackage ` to avoid repeating the same path
121
+ time and again::
131
122
132
123
use Symfony\Component\Asset\PathPackage;
124
+ // ...
133
125
134
126
$package = new PathPackage('/static/images', new StaticVersionStrategy('v1'));
135
127
@@ -140,11 +132,12 @@ Request Context Aware Assets
140
132
............................
141
133
142
134
If you are also using the HttpFoundation component in your project, for example
143
- in a Symfony application, the `` PathPackage `` class can take into account the
144
- context of the current request::
135
+ in a Symfony application, the :class: ` Symfony \C omponent \A sset \ P athPackage ` class
136
+ can take into account the context of the current request::
145
137
146
138
use Symfony\Component\Asset\PathPackage;
147
139
use Symfony\Component\Asset\Context\RequestStackContext;
140
+ // ...
148
141
149
142
$package = new PathPackage('/static/images', new StaticVersionStrategy('v1'));
150
143
$package->setContext(new RequestStackContext($requestStack));
@@ -153,29 +146,34 @@ context of the current request::
153
146
// result: /somewhere/static/images/logo.png?v1
154
147
155
148
When the request context is set, in addition to the configured base path,
156
- `` PathPackage `` also prepends the current request base URL (`` /somewhere/ `` in
157
- this example) to assets. This allows your website to be hosted anywhere under
158
- the web server root directory.
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.
159
152
160
153
Absolute Assets and CDNs
161
154
~~~~~~~~~~~~~~~~~~~~~~~~
162
155
163
156
Applications that host their assets on different domains and CDNs (*Content
164
- Delivery Networks *) should use instead the `` UrlPackage `` class to generate
165
- absolute URLs for their assets::
157
+ Delivery Networks *) should use the :class: ` Symfony \C omponent \A sset \ U rlPackage `
158
+ class to generate absolute URLs for their assets::
166
159
167
160
use Symfony\Component\Asset\UrlPackage;
161
+ // ...
168
162
169
- $package = new UrlPackage('http://static.example.com/images/', new StaticVersionStrategy('v1'));
163
+ $package = new UrlPackage(
164
+ 'http://static.example.com/images/',
165
+ new StaticVersionStrategy('v1')
166
+ );
170
167
171
168
echo $package->getUrl('/logo.png');
172
169
// result: http://static.example.com/images/logo.png?v1
173
170
174
171
In case you serve assets from more than one domain to improve application
175
- performance, pass an array of URLs as the first argument of `` UrlPackage ``
176
- constructor::
172
+ performance, pass an array of URLs as the first argument of
173
+ :class: ` Symfony \C omponent \A sset \U rlPackage ` constructor::
177
174
178
175
use Symfony\Component\Asset\UrlPackage;
176
+ // ...
179
177
180
178
$urls = array(
181
179
'http://static1.example.com/images/',
@@ -200,8 +198,12 @@ protocol-relative URLs for HTTPs requests, any base URL for HTTP requests)::
200
198
201
199
use Symfony\Component\Asset\UrlPackage;
202
200
use Symfony\Component\Asset\Context\RequestStackContext;
201
+ // ...
203
202
204
- $package = new UrlPackage(array('http://example.com/', 'https://example.com/'), new StaticVersionStrategy('v1'));
203
+ $package = new UrlPackage(
204
+ array('http://example.com/', 'https://example.com/'),
205
+ new StaticVersionStrategy('v1')
206
+ );
205
207
$package->setContext(new RequestStackContext($requestStack));
206
208
207
209
echo $package->getUrl('/logo.png');
@@ -212,7 +214,8 @@ Named Packages
212
214
213
215
Applications that manage lots of different assets may need to group them in
214
216
packages with the same versioning strategy and base path. The Asset component
215
- includes a ``Packages `` class to simplify the management of several packages.
217
+ includes a :class: `Symfony\C omponent\A sset\P ackages ` class to simplify the
218
+ management of several packages.
216
219
217
220
In the following example, all packages use the same versioning strategy, but
218
221
they all have different base paths::
@@ -221,6 +224,7 @@ they all have different base paths::
221
224
use Symfony\Component\Asset\PathPackage;
222
225
use Symfony\Component\Asset\UrlPackage;
223
226
use Symfony\Component\Asset\Packages;
227
+ // ...
224
228
225
229
$versionStrategy = new StaticVersionStrategy('v1');
226
230
@@ -233,11 +237,11 @@ they all have different base paths::
233
237
234
238
$packages = new Packages($defaultPackage, $namedPackages)
235
239
236
- The `` Packages `` class requires to define a default package which will be applied
237
- to all assets except those which indicate the name of the package to use. In
238
- addition, this application defines a package named `` img `` to serve images from
239
- an external domain and a ``doc `` package to avoid repeating long paths when
240
- linking to a document inside a template::
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::
241
245
242
246
echo $packages->getUrl('/main.css');
243
247
// result: /main.css?v1
0 commit comments