odan/plates-asset-cache
最新稳定版本:2.1.0
Composer 安装命令:
composer require odan/plates-asset-cache
包简介
Caching and compression for Plates template assets (JavaScript and CSS).
README 文档
README
Caching and compression for Plates template assets (JavaScript and CSS).
Installation
composer require odan/plates-asset-cache
Requirements
- PHP 7.3+ or 8.0+
Configuration
use League\Plates\Engine; use Odan\PlatesAsset\AssetEngine; use Odan\PlatesAsset\PlatesAssetExtension; use Symfony\Component\Cache\Adapter\FilesystemAdapter; $engine = new Engine('/path/with/html/templates'); $options = [ // Public assets cache directory 'path' => '/var/www/example.com/htdocs/public/assets/cache', // Public cache directory permissions (octal) // You need to prefix mode with a zero (0) // Use -1 to disable chmod 'path_chmod' => 0750, // The public url base path 'url_base_path' => 'assets/cache/', // Internal cache settings // // The main cache directory // Use '' (empty string) to disable the internal cache 'cache_path' => '/var/www/example.com/htdocs/temp', // Used as the subdirectory of the cache_path directory, // where cache items will be stored 'cache_name' => 'assets-cache', // The lifetime (in seconds) for cache items // With a value 0 causing items to be stored indefinitely 'cache_lifetime' => 0, // Enable JavaScript and CSS compression // 1 = on, 0 = off 'minify' => 1 ]; // Register asset extension $engine->loadExtension(new PlatesAssetExtension(new AssetEngine($engine, $options)));
Usage
The page template
Template file: index.php
<?php /** @var League\Plates\Template\Template $this */ ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <base href="<?= $baseUrl; ?>" /> <title>Demo</title> <?= $this->assets(['default.css', 'print.css'], ['inline' => true]); ?> </head> <body> <!-- content --> <!-- JavaScript assets --> <?= $this->assets(['mylib.js', 'page.js']); ?> </body> </html>
Render a template
$engine = new League\Plates\Engine('/path/to/templates'); echo $engine->render('index', ['baseUrl' => '']);
The result
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <base href="" /> <title>Demo</title> <style></style> <style>@media print{.noprint{display:none}.navbar{visibility:hidden;display:none}.container{width:99%}.table{table-layout:fixed;width:99%;max-width:99%}}</style></head> <body> <!-- content --> <!-- JavaScript assets --> <script src="assets/file.3dd5380c0b893eea8a14e30ce5bfa4cb9aab011b.js"></script></body> </html>
Parameters
- Parameter: $assets
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| files | array | [] | yes | All assets (files) to be delivered to the browser. Plates Folders (myalias::myfile.js) are also supported. |
- Parameter: $options
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| inline | bool | false | no | Defines whether the browser downloads the assets inline or via URL. |
| minify | bool | true | no | Specifies whether JS/CSS compression is enabled or disabled. |
| name | string | file | no | Defines the output file name within the URL. |
Slim 4 integration
For this example we use the PHP-DI package.
Add the container definition:
<?php use League\Plates\Engine; use Odan\PlatesAsset\PlatesAssetExtension; use Psr\Container\ContainerInterface; use Slim\App; // ... return [ // ... Engine::class => function (ContainerInterface $container) { $settings = $container->get('settings'); $viewPath = $settings['plates']['path']; $engine = new Engine($viewPath); // The public url base path $baseUrl = $container->get(App::class)->getBasePath(); $engine->addData(['baseUrl' => $baseUrl]); $options['url_base_path'] = $basePath; $engine->loadExtension(new PlatesAssetExtension(new AssetEngine($engine, $options))); return $engine; }, ];
Render the template and write content to the response stream:
$response->withHeader('Content-Type', 'text/html; charset=utf-8'); $response->getBody()->write($this->engine->render($name, $viewData)); return $response;
License
The MIT License (MIT). Please see License File for more information.
odan/plates-asset-cache 适用场景与选型建议
odan/plates-asset-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.44k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「assets」 「javascript」 「cache」 「css」 「minify」 「plates」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 odan/plates-asset-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 odan/plates-asset-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 odan/plates-asset-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Asset Management for PHP
repository php library
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 2.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-06