<code>[root@aliyun assets]# pwd /home/flxx/basic/web/assets [root@aliyun assets]# ll total 60 drwxrwxrwx 7 root root 4096 Jul 29 13:26 1fe421c3 drwxrwxr-x 7 apache apache 4096 Jul 29 13:39 349e6026 drwxrwxrwx 5 root root 4096 Jul 29 13:26 40473b7f drwxrwxr-x 5 apache apache 4096 Jul 29 13:31 4939b2cb drwxrwxr-x 3 apache apache 4096 Jul 29 13:39 55993b1e drwxrwxrwx 7 root root 4096 Jul 29 13:26 68e31155 drwxrwxrwx 5 root root 4096 Jul 29 13:26 7007e07 drwxrwxrwx 2 root root 4096 Jul 29 13:26 770d3be4 drwxrwxrwx 3 root root 4096 Jul 29 13:26 7ee37afb drwxrwxrwx 3 root root 4096 Jul 29 13:26 9e44a6d drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a51215f4 drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a53efc6f drwxrwxrwx 2 root root 4096 Jul 29 13:26 bfcc54b5 drwxrwxrwx 2 root root 4096 Jul 29 13:26 c8cb6423 drwxrwxrwx 2 root root 4096 Jul 29 13:26 e006560 [root@aliyun assets]# </code>
为什么yii2 要在 web/asset 目录下生成这样的缓存文件?(很多MVC都有这个趋势) 且生成的CSS JS缓存文件,并没有做minify,里面注释还是原样保留。 这些缓存目录文件,何时清除?是否是自动清除?
回复内容:
<code>[root@aliyun assets]# pwd /home/flxx/basic/web/assets [root@aliyun assets]# ll total 60 drwxrwxrwx 7 root root 4096 Jul 29 13:26 1fe421c3 drwxrwxr-x 7 apache apache 4096 Jul 29 13:39 349e6026 drwxrwxrwx 5 root root 4096 Jul 29 13:26 40473b7f drwxrwxr-x 5 apache apache 4096 Jul 29 13:31 4939b2cb drwxrwxr-x 3 apache apache 4096 Jul 29 13:39 55993b1e drwxrwxrwx 7 root root 4096 Jul 29 13:26 68e31155 drwxrwxrwx 5 root root 4096 Jul 29 13:26 7007e07 drwxrwxrwx 2 root root 4096 Jul 29 13:26 770d3be4 drwxrwxrwx 3 root root 4096 Jul 29 13:26 7ee37afb drwxrwxrwx 3 root root 4096 Jul 29 13:26 9e44a6d drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a51215f4 drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a53efc6f drwxrwxrwx 2 root root 4096 Jul 29 13:26 bfcc54b5 drwxrwxrwx 2 root root 4096 Jul 29 13:26 c8cb6423 drwxrwxrwx 2 root root 4096 Jul 29 13:26 e006560 [root@aliyun assets]# </code>
为什么yii2 要在 web/asset 目录下生成这样的缓存文件?(很多MVC都有这个趋势) 且生成的CSS JS缓存文件,并没有做minify,里面注释还是原样保留。 这些缓存目录文件,何时清除?是否是自动清除?
不会自动清除,为什么要生成这样的缓存文件?这个问题问的不对,因为这些不是缓存。
composer大行其道的时代,有很多人实现某一个特殊功能打包发布,开发者只要通过composer拿回来就可直接使用。
比如yii2-admin,它是一套yii权限管理的模块,自带了前端界面,他的资源放在自己的源代码目录下,当开发者使用时,yii将这个模块用到的前端资源发布到web/assets目录下,而不用开发者做其他额外的工作。
这些发布的资源是可以被压缩的,使用yii的 assets 命令,详情可以看这里assets
一.yii的资源包(assets)
Yii在资源包中管理资源,资源包简单的说就是放在一个目录下的资源集合, 当在视图中注册一个资源包,在渲染Web页面时会包含其中中的CSS和JavaScript文件。
二.定义资源包:
见assets/AppAsset.php文件,定义基础应用模板使用的主要资源包:
<code>namespace app\assets; use yii\web\AssetBundle; class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; public $css = [ 'css/site.css', ]; public $js = [ ]; public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ]; } </code>
三.使用资源包
<code>为使用资源包,在视图中调用[[yii\web\AssetBundle::register()]]方法先注册资源, 例如,在视图模板可使用如下代码注册资源包: use app\assets\AppAsset; AppAsset::register($this); // $this 代表视图对象 </code>
如果在其他地方注册资源包,应提供视图对象,如在 小部件 类中注册资源包, 可以通过 $this->view 获取视图对象。
当在视图中注册一个资源包时,在背后Yii会注册它所依赖的资源包。
如果资源包是放在Web不可访问的目录下,会被发布到可访问的目录, 后续当视图渲染页面时,会生成这些注册包包含的CSS和JavaScript文件对应的 和<script> 标签</script>
四.资源发布
如前所述,如果资源包放在Web不能访问的目录,当视图注册资源时资源会被拷贝到一个Web可访问的目录中, 这个过程称为资源发布,[[yii\web\AssetManager|asset manager]]会自动处理该过程。
资源默认会发布到@webroot/assets目录,对应的URL为@web/assets, 可以配置[[yii\web\AssetManager::basePath|basePath]] 和 [[yii\web\AssetManager::baseUrl|baseUrl]] 属性自定义发布位置。见yii\web\AssetManager:
<code> public $bundles = []; public $basePath = '@webroot/assets'; public $baseUrl = '@web/assets'; </code>
除了拷贝文件方式发布资源,如果操作系统和Web服务器允许可以使用符号链接,该功能可以通过设置 [[yii\web\AssetManager::linkAssets|linkAssets]] 为 true 来启用。
<code>return [ // ... 'components' => [ 'assetManager' => [ 'linkAssets' => true, ], ], ]; </code>
使用以上配置,资源管理器会创建一个符号链接到要发布的资源包源路径,这比拷贝文件方式快并能确保发布的资源一直为最新的。
五.可见,asset目录并不是缓存目录,而是在访问页面时,视图要注册资源->发布资源.
在发布资源时,yii2的gii,debug这些资源都是在vendor目录下的,它们是web不能访问的目录,这个时候yii2会把资源发布到assets目录下的一个随机的字符串里,这样就可供web访问了。

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)