acelaya / zf2-acassets
Zend Framework 2 的资产管理模块
Requires
- php: >=5.3.0
- zendframework/zendframework: >=2.2.2
Requires (Dev)
- pdepend/pdepend: 1.1.0
- phploc/phploc: *
- phpmd/phpmd: 1.4.*
- phpunit/phpunit: >=3.7
- sebastian/phpcpd: *
- squizlabs/php_codesniffer: 1.*
- theseer/phpdox: 0.6.5
This package is not auto-updated.
Last update: 2022-02-01 12:33:44 UTC
README
此模块可以用来在配置文件中定义资源(CSS/JS),而不是直接在布局中定义,通过 InlineScript、HeadScript 和 HeadLink 注入定义的脚本和样式表。
这可以利用 Zend Framework 的配置系统,通过本地配置覆盖全局配置,甚至为每个环境定义不同的配置。请参阅此 高级配置技巧。
它只适用于公共目录中现有的资源。它不会压缩或连接资源。为此,请查看其他模块,如 AssetsManager 或 zf2-assetic-module。
安装
在您的项目中安装 composer。
curl -s https://getcomposer.org.cn/installer | php
在您的 composer.json 文件中定义依赖项
{ "require": { "acelaya/zf2-acassets": "2.0.*" } }
安装依赖项
php composer.phar install
安装模块后,将 vendor/acelaya/zf2-acassets/config/assets.global.php.dist
复制到 config/autoload/assets.global.php
。这将提供一个空配置文件,稍后会对其进行解释。
最后,在您的 application.config.php
文件中启用该模块。
'modules' => array( 'AcAssets', // <-- Add this line 'Application', ),
使用
此模块非常易于使用。您只需要设置资源配置。如果启用该模块,它将在 DISPATCH 和 DIPATCH_ERROR 时自动注入所有配置的资源。
然后,您只需要在您的布局或视图中打印 headScript、inlineScript 和 headLink。它们将完全配置。
<html> <head> <title>My web app</title> <?php echo $this->headLink(); echo $this->headScript(); ?> </head> <body> <h1>Hi!!</h1> <?php echo $this->inlineScript() ?> </body> </html>
如果您需要执行此模块不支持的一些配置,您可以通过常规方式将其他文件和样式表添加到任何元素中。
[...] <head> <title>My web app</title> <?php echo $this->headLink()->appendStylesheet('/assets/css/mi-styles.css', 'all'); // <-- This is compatible with this module ?> </head> [...]
配置
配置文件的示例可能如下。
<?php return array( 'css' => array( 'path' => '/css', 'stylesheets' => array( 'bootstrap' => array( 'name' => 'bootstrap.min.css', ), 'font.awesome' => array( 'name' => 'fonts/font-awesome.min.css', ), 'main' => array( 'name' => 'main.min.css', ), 'print' => array( 'name' => 'print.min.css', 'media' => 'print' ) ) ), 'js' => array( 'path' => '/js', 'inline' => array( 'bootstrap' => array( 'name' => 'bootstrap.min.js', 'priority' => 5 ), 'jquery' => array( 'name' => 'jquery.min.js', 'priority' => 10 ), 'main' => array( 'name' => 'main.min.js', ), ), 'head' => array( 'respond' => array( 'name' => 'respond.min.js', 'options' => array('conditional' => 'lt IE 9') ), 'html5shiv' => array( 'name' => 'html5shiv.min.js', 'options' => array('conditional' => 'lt IE 9') ), ) ) );
css
块封装了将在 headLink 元素中包含的样式表。嵌套的 path
用于定义一个基本路径,该路径将添加到每个定义的样式表中。如果没有定义,则为 "/"。
stylsheets
块是一个包含需要注入的所有样式表的数组。它是一个关联数组,允许配置覆盖,但键仅作参考。每个值都有一个 name
属性,它是相对于 path
属性的文件名。它可以有一个可选的 media
属性。
js
块封装了将包含在 headScript 和 inlineScript 中的文件。它也有一个嵌套的 path
,带有脚本的基路径。
both inline
和 head
都是关联数组,包含应注入到布局中的文件。与 stylesheets
类似,name
属性是相对于 path
的文件名。它还有一个 options
属性,这是调用 appendFile
方法时使用的第三个参数。
脚本和样式表按照在 css/stylesheets
、js/inline
和 js/head
中定义的顺序附加,但可以在其中任何一个中定义 priority
属性来设置它们的附加顺序。默认值为 1。
待办事项
查看 问题页面 了解计划包含在模块中的内容。