vcomedia/vco-zf-mediapath
Composer 安装命令:
composer require vcomedia/vco-zf-mediapath
包简介
Zend Framework 2 basePath view helper wrapper.
README 文档
README
ZF2 Cachebuster is a cache buster module closely modeled after https://github.com/gordonknoppe/magento-cachebuster. It facilitates automatic purging of static assets from HTTP caches such as browser cache, CDN, Varnish, etc using best practices outlined within the HTML5 boilerplate community.
See section "Filename-based cache busting" in: https://github.com/h5bp/server-configs-apache/blob/2.14.0/dist/.htaccess#L968
Overview
The module provides cachebusting by automatically altering the URI created by Magento for static files by adding the timestamp of the file to the filename:
Example uses
- Automatically invalidating cache when using Cloudfront CDN
- Amazon's Cloudfront CDN can be configured to use an origin server but by it's nature will not refresh your updated file until it's cache time expires or you send an invalidation request using their API.
- No more browser cache issues (ie. Them: "Where's that CSS change I requested?". You: "Oh, did you hit refresh?")
- Adding far-future expires headers, which is good for reducing the number of requests to your server, means that even without a CDN you have probably experienced browser cache causing a waste of time on what turns out to be a non-issues.
Installation
Composer
- Install Composer
- Install the module using Composer into your application's vendor directory. Add the following line to your
composer.json.
{
"require": {
"vcomedia/vco-zf-mediapath": "dev-master"
}
}
- Execute
composer update - Enable the module in your ZF2
application.config.phpfile.
return array( 'modules' => array( 'VcoZfMediaPath' ) );
- Copy and paste the
vco-zf-logger/config/module.vco-zf-mediapath.local.php.distfile to yourconfig/autoloadfolder and customize it with your configuration settings. Make sure to remove.distfrom your file. Yourmodule.vco-zf-mediapath.local.phpmight look something like the following:
<?php /** * VcoZfMediaPath - Zend Framework 2 basePath view helper wrapper. * * @category Module * @package VcoZfMediaPath * @author Vahag Dudukgian (valeeum) * @license http://opensource.org/licenses/MIT The MIT License (MIT) * @link http://github.com/vcomedia/vco-zf-mediapath/ */ namespace VcoZfMediaPath; return array( 'VcoZfMediaPath' => array( 'docRoot' => '/public', 'cdnOptions' => array( 'enabled' => true, 'defaultDomain' => array( 'http' => 'http://cdn.domain.com', 'https' => 'http://cdn.domain.com' ) ), 'cacheBusterOptions' => array( 'enabled' => true, 'basePaths' => array( '/skin/', '/js/', '/scripts/', '/css/', '/stylesheets/', '/media/', '/uploads/', '/images/', '/img/', '/imgs/', '/cache/', '/fonts/' ), 'extensions' => array( 'js', 'css', 'jpg', 'jpeg', 'gif', 'png', 'ttf', 'woff', 'pdf' ) ) ) );
Note: The configuration array returned by the top level 'VcoZfLogger' key is passed directly into the Log class constructor with the exception of the mail transport and mongo credential injection which are both optional.
mod_rewrite configuration
The following mod_rewrite rules need to be enabled for your store when using this module, potentially via .htaccess file or Virtualhost definition.
<IfModule mod_rewrite.c>
############################################
## rewrite files for magento cachebuster
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpeg|jpg|gif)$ $1.$3 [L]
</IfModule>
nginx configuration
For nginx you will need to add a rule like the following to your site definition.
location ~* (.+)\.(\d+)\.(js|css|png|jpg|jpeg|gif)$ {
try_files $uri $1.$3;
}
Duplicate Content Considerations and Restricting to Specific Static Folders When Using AWS Cloudfront (approach can apply to any CDN)
- Create 2 origins:
- One origin would be "domain.com"
- The other origin would be a custom error page such as "domain.com/403.html" or however you want to handle this aspect.
- Create Cache Behaviors for each static file folder you have. For example:
- For "/css" you would create a Cache Behavior path pattern of "css/*"
- For "/js" you would create a Cache Behavior path pattern of "js/*"
- etc.
- For each of those Cache Behaviors, you would then want to make sure that the Origin you specify is for "domain.com".
- That way, any request for "http://cdn.domain.com/css/" etc will be similar to making a request to "http://domain.com/css/"
- Then for the Default Cache Behavior (*), you can point that to the second origin you had created. Following the example from Step 1, that origin would be "domain.com/403.html".
So essentially, how the above would work is that any request to http://cdn.domain.com/css/, /js/, etc; it will go to your origin appropriately. If they try and go to "http://cdn.domain.com/notspecified/", that will only match the Default Cache Behavior (*) which will then point them to the 403 page you have created. That should make it so anything that crawls cdn.domain.com should only see your static content and nothing else if it wasn't specified in your Cache Behavior path patterns.
This will at the very least restrict duplicate content issues to static files.
License
Licensed under the Apache License, Version 2.0
vcomedia/vco-zf-mediapath 适用场景与选型建议
vcomedia/vco-zf-mediapath 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.14k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 04 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「zf2」 「basepath」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vcomedia/vco-zf-mediapath 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vcomedia/vco-zf-mediapath 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vcomedia/vco-zf-mediapath 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A URL base path detector for Slim 4
LosLog provides some log utility
EdpModuleLayouts is very simple Laminas module for making module-specific layouts insanely easy.
Zend Framework module to leverage the symfony dependency injection container
RCM User HTML views/pages
Builds a Bridge between Zend Expressive and Plugin Managers of Zend\MVC
统计信息
- 总下载量: 5.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-06