为ZF2/ZF3/Laminas提供的CDN助手

2.5.1 2022-11-26 08:17 UTC

This package is auto-updated.

Last update: 2024-08-26 11:52:23 UTC


README

版本 2.* 由Rostislav Mykhajliw创建 构建状态

简介

TweeCdn 是一组视图助手,用于支持根据规则转换 css/js/图片链接。

要求

  • php >= 7
  • zendframework/zend-stdlib
  • zendframework/zend-view

特性/目标

  • 简单:通过在文件末尾添加 ?timestamp 标记,例如 /css/test.css?1234567
  • 发布标识:通过给定的 REVISION 号标识新的静态文件版本,例如 /css/1234/test.css
  • 哈希:基于文件 MD5 哈希的唯一标记,例如 /css/af34c42/test.css。它允许用户只加载更改过的文件。
  • 多主机名支持:例如 host1.com, host2.com
  • 哈希生成器

安装

主要设置

使用 composer

  1. 在您的 composer.json 中添加此项目和 Cdn

    "require": {
        "necromant2005/cdn": "*",
    }
  2. 现在通过运行以下命令告诉 composer 下载 TweeCdn

    $ php composer.phar update

安装后

在您的 "application.config.php" 文件中启用它。

```php
<?php
return array(
    'modules' => array(
        // ...
        'TweeCdn',
    ),
    // ...
);
```

高级配置

  • type - cdn 类型
  • public_dir - 公共目录路径
  • hostnames - 可用主机名列表
  • hashes - 哈希列表(file_path => hash)
  • mapping - 将特定文件路径映射到全局 CDN:Google/NetDNA/MaxCDN

Options hostnames - 支持所有 CDN 助手。

  1. 简单(默认模式):简单模式在文件末尾添加 ?timestamp 标记,例如 Original

        /css/simple.css

    转换为

        /css/simple.css?1353231966

    将 jQuery 映射到 Google CDN 的配置(https://developers.google.com/speed/libraries/devguide

    <?php
    return array(
        'di' => array(
            'instance' => array(
                'TweeCdn\View\Helper\Cdn' => array(
                    'parameters' => array(
                        // simple configuration
                        'type'    => 'simple',
                        'options' => array(
                            'public_dir' => __DIR__ . '/../../../../public',
                            'mappings' => array(
                                '/js/jquery.js' => '//ajax.googleapis.ac.cn/ajax/libs/jquery/1.8.3/jquery.min.js'
                            ),
                        ),
                    ),
                ),
            ),
        ),
    );
  2. 发布:基于发布的助手将存储在文件 "/RELEASE" 中的版本号放置在第一个文件夹名之后。结果是在每次发布时都有一个唯一的路径。(默认情况下,"RELEASE" 文件由 远程多服务器自动化工具 - Capistrano 创建)Original

        /css/simple.css

    转换为

        /css/1353231966/simple.css

    配置

    <?php
    return array(
        'di' => array(
            'instance' => array(
                'TweeCdn\View\Helper\Cdn' => array(
                    'parameters' => array(
                        // simple configuration
                        'release'    => 'release',
                        'options' => array(
                            'public_dir' => __DIR__ . '/../../../../public',
                            'release' => trim(file_get_contents(__DIR__ . '/../../../../REVISION')),
                        ),
                    ),
                ),
            ),
        ),
    );
  3. 哈希:哈希提供了与 "发布" 类似的功能,但使用唯一的文件内容哈希。它可以在两种模式下工作

    • 动态 - 当哈希即时生成时
    • 预编译 - 使用现有的哈希映射。对于 "预编译" 模式,您可以使用脚本生成哈希映射文件。它创建 tmp/hashes.php 文件列表。最终,它使应用程序运行更快,因为您通过跳过每次请求中的文件 MD5 计算来减少 I/O 磁盘操作。
    vendor/bin/hash_collector.php
    

    Original

        /css/simple.css

    转换为

        /css/72e7d8fb348a326251c37821d1b6bfe16ea69d6e/simple.css

    带有故障转移保护的配置示例,以处理缺失的 tmp/hashes.php 文件和主机名。这些文件将通过 cdn-0 和 cdn-1 随机分发,具体取决于文件名。

    <?php
    return array(
        'di' => array(
            'instance' => array(
                'TweeCdn\View\Helper\Cdn' => array(
                    'parameters' => array(
                        // simple configuration
                        'release'    => 'hash',
                        'options' => array(
                            'public_dir' => __DIR__ . '/../../../../public',
                            'hostnames' => array('http://cdn-0.coockieless.domain.com', 'http://cdn-1.coockieless.domain.com'),
                            'hashes' => (file_exists(__DIR__ . '/../tmp/hashes.php')) ? include __DIR__ . '/../tmp/hashes.php' : array(),
                        ),
                    ),
                ),
            ),
        ),
    );