aura / asset-bundle
aura v2 的资产管理
Requires
- aura/web: 2.*
This package is auto-updated.
Last update: 2024-09-11 04:23:41 UTC
README
PHP 的资产管理。
前言
要求
此包需要 PHP 5.3 或更高版本。与 Aura 库包不同,此资产包有用户空间依赖项
安装
此 asset-bundle 可以通过 Composer 的以下 require
元素安装和自动加载:在你的 composer.json
文件中
"require": {
"aura/asset-bundle": "2.*"
}
测试
composer install phpunit -c tests/unit
PSR 兼容性
此内核试图遵守 PSR-1、PSR-2 和 PSR-4。如果您注意到兼容性遗漏,请通过拉取请求发送补丁。
社区
要提问、提供反馈或与 Aura 社区进行其他沟通,请加入我们的 Google Group,关注 @auraphp on Twitter,或在 Freenode 上与我们聊天 #auraphp。
包结构
假设你有一个 Vendor.Package
。你所有的资产都应该在 web
文件夹中。文件夹名称 css
、images
、js
可以根据你的喜好命名。
├── src │ ├── Cli │ └── Web ├── tests └── web ├── css │ └── some.css ├── images │ ├── another.jpg │ └── some.png └── js └── hello.js
假设你有相同的结构,现在在你的模板中,你可以指向 /asset/vendor/package/css/some.css
、/asset/vendor/package/js/hello.js
、/asset/vendor/package/images/another.jpg
。
你需要确保在名称 asset/vendor/package
中的一项是
vendor/package
是 composer 包名。
在任何项目中的使用
根据你使用的路由器,将路径添加到路由器中,以便可以从其中提取 vendor、package 和文件名。
以下是一个使用 Aura.Router 和 Aura.Dispatcher 的示例。由于需要递归调用 __invoke
方法,因此使用了 dispacther。否则,操作将返回 responder,然后你需要调用 responder 以获取响应,最后发送响应。
<?php $map = array( 'my/package' => '/path/to/web/where/css/js/etc/', 'my/package2' => '/path/to/web/where/css/js/etc/of/packag2' ); $types = array(); $router->add('aura.asset', '/asset/{vendor}/{package}/{file}') ->setValues([ 'action' => 'aura.asset', ]) ->addTokens(array( 'file' => '(.*)' )); $dispatcher->setObject( 'aura.asset', function () use ($map, $types) { $action = new \Aura\Asset_Bundle\AssetAction( new \Aura\Asset_Bundle\AssetService($map, $types), new \Aura\Asset_Bundle\AssetResponder() ); return $action; } );
在你的布局或视图中
<link href="/asset/<vendor>/<package>/css/bootstrap.min.css" rel="stylesheet">
Aura.Web_Kernel 中的使用
<?php // more code public function define(Container $di) { $di->params['Aura\Asset_Bundle\AssetService']['map']['cocoframework/example'] = dirname(__DIR__) . '/web'; }
确保你已经为 Aura.View 定义了路由器助手。
<link rel="stylesheet" href="<?php echo $this->router() ->generateRaw('aura.asset', array( 'vendor' => 'cocoframework', 'package' => 'example', 'file' => '/css/syntax.css' ) ); ?>">