The reason I want to introduce the Symfony Asset component is because I estimate that this component is one of the most easily overlooked components in the Symfony package. I've never looked at the documentation before, and I've never used any of the relevant features until recently when the company's website had to do CDN acceleration, only to think of something like that.
Perhaps a lot of students learn Symfony more or less in the Symfony official best practice tutorials have seen asset this twig function, but the document does not explain what asset is doing, can bring what kind of benefits. I recently read the documentation for the Asset component and have done some practical work to share some useful information.
What is Asset?
is actually the css/javascript/picture such static file collectively.
Common problems encountered by Asset management and maintenance
In the past, the path of static files are written in template files, such as the official website provides examples:
Unless it's really a fairly simple project you can write about, you'd better not write these paths down, because you'll be digging up the following holes for yourself:
- You have to keep writing the full path in every place that references a static file.
- Version management trouble. You may want to cache your static files, while caching static files requires you to control whether the cache is updated by adding a version number (typically similar to/main.css?v=1) after the URL address, but if your path is written in each file ... Then you will be one by one slowly change, also easy to leak.
- Project just started static file path write to die, in case the path of static file directory changed you silly fork, another one slowly change it.
- One technique, called multi-CDN, is to relieve the pressure on a single CDN. If the file Main.css did two CDN Http://cdn1.com/main.css and HTTP://CDN2.COM/MAIN.CSS, if the path is dead, how do you get the page to randomly return one of these two CDN addresses?
What is the Symfony Asset component capable of?
Corresponds to a few of the above questions:
- With it, you don't have to write the path all over again, but you can omit the common directory part.
- With it, specifying the static file version is convenient, just a configuration thing!
- With it, the location of mobile static files convenient, change directory for the domain name for CDN, casually change!
- With it, the realization of multi-CDN is not a dream!
I'll write it here today. If everyone is from the non-my personal site (http://chrisyue.com) See the Reprint, please click the original text to see if the follow-up has been completed.
Symfony Asset component So good, how to use it?
I'm using the Symfony framework, so how do I use the Symfony Asset component in the framework?
Weak weak more Ask a sentence: Symfony assetic component is what ghost, with Asset components have what relationship?