Assert Component
assert component manages url generation and versioning of web asset such as
css, js and images.
in the past, it was common for web applications to hardcode urls of web assets(资源).
for example:
<link rel="stylesheet" type="text/css" href="/css/main.css">
缺点:
模板冗余
版本控制困难
移动资源的位置是非常繁琐而且极其容易出错
加载多个cdn几乎不可能
Usage
Asset Packages
Versioned Assets
one of the main features of assert component is the ability to manage the versioning
of the application's assets.Asset versions are commonly used to control how these
assets are cached.不是依赖于简单的版本机制,asset component允许你定义先进的版本控制策略.
这两个机制是EmptyVersionStrategy,不添加任何版本到资源,StaticVersionStrategy,允许你用格式的字符串
设置一个版本.
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
$package = new Package(new EmptyVersionStrategy());
echo $package->getUrl('/image.png');
// result: /image.png
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
$package = new Package(new StaticVersionStrategy('v1'));
echo $package->getUrl('/image.png');
// result: /image.png?v1
// put the 'version' word before the version value
$package = new Package(new StaticVersionStrategy('v1', '%s?version=%s'));
echo $package->getUrl('/image.png');
// result: /image.png?version=v1
// put the asset version before its path
$package = new Package(new StaticVersionStrategy('v1', '%2$s/%1$s'));
echo $package->getUrl('/image.png');
// result: /v1/image.png
assert component manages url generation and versioning of web asset such as
css, js and images.
in the past, it was common for web applications to hardcode urls of web assets(资源).
for example:
<link rel="stylesheet" type="text/css" href="/css/main.css">
缺点:
模板冗余
版本控制困难
移动资源的位置是非常繁琐而且极其容易出错
加载多个cdn几乎不可能
Usage
Asset Packages
Versioned Assets
one of the main features of assert component is the ability to manage the versioning
of the application's assets.Asset versions are commonly used to control how these
assets are cached.不是依赖于简单的版本机制,asset component允许你定义先进的版本控制策略.
这两个机制是EmptyVersionStrategy,不添加任何版本到资源,StaticVersionStrategy,允许你用格式的字符串
设置一个版本.
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
$package = new Package(new EmptyVersionStrategy());
echo $package->getUrl('/image.png');
// result: /image.png
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
$package = new Package(new StaticVersionStrategy('v1'));
echo $package->getUrl('/image.png');
// result: /image.png?v1
// put the 'version' word before the version value
$package = new Package(new StaticVersionStrategy('v1', '%s?version=%s'));
echo $package->getUrl('/image.png');
// result: /image.png?version=v1
// put the asset version before its path
$package = new Package(new StaticVersionStrategy('v1', '%2$s/%1$s'));
echo $package->getUrl('/image.png');
// result: /v1/image.png