如何在运行中编译时在LESS PHP中指定相对路径


How to specify relative path in LESS PHP when compiling on the run?

最近我开始使用LESS为多个项目创建通用CSS。由于它将在多个项目中使用,它作为git子模块包含在我当前的项目中,所有内容都放在一个文件夹中。所以整个项目文件看起来是这样的:

myProject
    app
        //this is the code of my project
    public
        css
          //storing some third party CSS like font awesome
        fonts
          //font files of third part CSS
        myCSS
            less
              myStyle.less
            fonts
              myFont.ttf
  Some Other files and folders

我使用laravel 4.2作为框架。起初,我开始使用less.js在运行中编译less文件,一切都很好。以下是包含字体的LESS。

@font-face{
   font-family: OpenSans-Regular;
   src: url('../fonts/myFont.ttf'); 
}

然而,我发现在一些较旧的机器和移动设备上,处理速度不够快,会有一个小瞬间,元素显示得毫无风格。因此,为了解决这个问题,我决定使用LESS PHP在服务器端编译该文件。以下是我放在模板文件顶部的内容。(每隔一页将包括以下行)

<head>
<?php
require "lessc.inc.php";
$less = new lessc;
echo $less->compileFile("myStyle.less");
?>
//some other js and css to include
</head>
<body>
    //page contents
</body>

它编译成功,但由于LESS是直接编译到页面上的,当我的页面有url时,

http://domain.com/public/myController/myPage/

它将在中搜索字体文件

http://domain.com/public/fonts/myFont.tff

应该是

http://domain.com/public/myCSS/fonts/myFont.tff

如何使用PHP正确编译LESS文件?我不想更改LESS文件中的路径,也不想将LESS文件放在子模块文件夹(即myCSS)之外,因为这个样式文件将在许多其他项目中重复使用。我想在运行中编译,因为LESS文件在开发过程中仍然会快速更改。

只需从根目录指定字体文件。

@font-face{
   font-family: OpenSans-Regular;
   src: url('/myCSS/fonts/myFont.ttf'); 
}