加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – CssRewrite过滤器不会将相对URL转换为绝对值

发布时间:2020-12-13 16:05:44 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试将所有使用过的css文件合并为一个.但包含相对路径的资源会导致问题.我尝试了资产的CssRewriteFilter,但它不起作用. 这是我的代码: $files = [ '/node_modules/select2/dist/css/select2.min.css','/node_modules/jquery-datetimepicker/jquery.da
我正在尝试将所有使用过的css文件合并为一个.但包含相对路径的资源会导致问题.我尝试了资产的CssRewriteFilter,但它不起作用.

这是我的代码:

$files = [
        '/node_modules/select2/dist/css/select2.min.css','/node_modules/jquery-datetimepicker/jquery.datetimepicker.css','/node_modules/icheck/skins/square/blue.css','/node_modules/Plupload/js/jquery.plupload.queue/css/jquery.plupload.queue.css','/node_modules/pace/themes/white/pace-theme-flash.css','/node_modules/lightgallery/dist/css/lightgallery.min.css','/node_modules/x-editable/dist/bootstrap3-editable/css/bootstrap-editable.css','/node_modules/sweetalert2/dist/sweetalert2.css','/node_modules/animate.css/animate.min.css','/node_modules/fullcalendar/dist/fullcalendar.min.css','/node_modules/flag-icon-css/css/flag-icon.min.css','/theme/assets/skin/default_skin/css/theme.css',];

    $am = new AsseticAssetManager();

    $items = [];

    foreach ($files as $file) {
        $filename = APP_ROOT_DIR . '/public/ui' . $file;
        $items[] = new AsseticAssetFileAsset($filename,[new AsseticFilterCssRewriteFilter()]);
    }

    $collection = new AsseticAssetAssetCollection(
        $items
    );
    $collection->setTargetPath('vendor.css');

    $am->set('collection',$collection);

    $writer = new AsseticAssetWriter(APP_ROOT_DIR . '/public/ui/shared/css/');
    $writer->writeManagerAssets($am);

我期待的是:

在/node_modules/test/css/test.css中的../img/test.png代码中包含img时,Assetic应该将其重写为/node_modules/test/img/test.png

解决方法

尝试构建FileAsset类,如:

new AsseticAssetFileAsset(
    $filename,[new AsseticFilterCssRewriteFilter()],'/node_modules/test/'
);

在src / Assetic / Asset / FileAsset.php:37中,我们看到您可以包含$sourcePath作为第三个参数.

https://github.com/kriswallsmith/assetic/blob/master/src/Assetic/Asset/FileAsset.php#L37

您可能希望将$files数组重新设置为类似的内容

$files = [
    ['/node_modules/select2/dist/css/','select2.min.css']
    ...
];

这样你就可以两次调用$sourcePath了

new AsseticAssetFileAsset(
    APP_ROOT_DIR . '/public/ui' . $file[0].$file[1],'/public/ui'.$filename[0]
);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读