Table of Contents
Asset 是什么东东?
Asset 管理维护会遇到的常见问题
那 Symfony Asset 组件能干啥?
Symfony Asset 组件这么棒,怎么用呢?
我用的是 Symfony 框架,那在框架里该如何使用 Symfony Asset 组件呢?
弱弱的多问一句:Symfony Assetic 组件又是什么鬼,跟 Asset 组件有什么关系吗?
Home Backend Development PHP Tutorial 使用 Symfony Asset 组件管理你的 CSS Javascript 和图片文件

使用 Symfony Asset 组件管理你的 CSS Javascript 和图片文件

Jun 23, 2016 pm 01:01 PM

之所以想介绍 Symfony Asset 组件,是因为我估计这个组件是 Symfony 组件包里可能最容易被轻视的组件之一了。我之前就从来没有正眼看过相关的文档,也从来没用过相关功能,直到最近公司网站要做 CDN 加速的时候,才想起来有这么一个东西。

可能很多学习 Symfony 的同学或多或少都在 Symfony 官方最佳实践教程里看到过 asset这个 twig 函数,但文档并没有解释 asset是做什么的,能带来什么样的好处。我最近仔细阅读了 Asset 组件的文档,并做了相关实践,把一些有用的信息分享一下。

Asset 是什么东东?

其实就是 css / javascript / 图片 这类静态文件的统称啦。

Asset 管理维护会遇到的常见问题

过去静态文件的路径都是写死在模板文件里的,比如官网提供的例子:

<linkrel="stylesheet" type="text/css" href="/css/main.css"> <!-- ... --> <ahref="/"><img  src="/static/imghw/default1.png"  data-src="/images/logo.png"  class="lazy" alt="使用 Symfony Asset 组件管理你的 CSS Javascript 和图片文件" ></a>
Copy after login

除非真的是一个相当相当简单的项目你可以这么写,你最好不要把这些路径写死,因为你会给你自己挖以下几个坑儿:

  1. 你要不断得在每一个引用静态文件的地方写全路径。
  2. 版本管理麻烦。你可能想要缓存你的静态文件,而缓存静态文件需要通过在 URL 地址后面添加版本号(一般来说类似于 /main.css?v=1的方式)来控制缓存是否要更新,但如果你的路径都是在各个文件里写死的……那你就一个一个慢慢改吧,还容易漏。
  3. 项目刚开始静态文件路径写死一时爽,万一静态文件目录的路径变了你就傻叉了,又一个一个慢慢改吧。
  4. 有一种技术叫做多 CDN,用来减轻单 CDN 的压力。假如文件 main.css做了两个 CDN http://cdn1.com/main.css和 http://cdn2.com/main.css,如果路径是写死的,你如何做到让页面随机返回这两个 CDN 地址中的其中一个?

那 Symfony Asset 组件能干啥?

正好对应于上面的几个问题:

  1. 有了它,你不用再把路径写全了,而是可以把公用的目录部分省略掉
  2. 有了它,指定静态文件版本方便了,就一个配置的事儿!
  3. 有了它,移动静态文件的位置方便了,换目录换域名换 CDN,随便换!
  4. 有了它,实现多 CDN 不是梦!

今天我先写到这儿吧。如果大家是从非我个人网站(http://chrisyue.com)看到的转载,请点原文看看后续有没有完成。

Symfony Asset 组件这么棒,怎么用呢?

我用的是 Symfony 框架,那在框架里该如何使用 Symfony Asset 组件呢?

弱弱的多问一句:Symfony Assetic 组件又是什么鬼,跟 Asset 组件有什么关系吗?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles