在 Symfony 中找不到字体文件的路径
No route found for font files in Symfony
我在 Symfony 中加载字体时遇到了一些困难。这是出了什么问题:
情况
我正在使用 Symfony,我正在尝试将 Metro UI CSS 文件添加到一个包中。这些文件位于 Resources/public/lib/metro-ui 文件夹中。
Resources/public/lib/metro-ui
- css
- fonts
- js
- min
我在 twig 文件中有这样的布局:
{% stylesheets
'bundles/manager/lib/metro-ui/css/metro-bootstrap.css'
%}
Metro UI 的 bootstrap 文件包含以下字体:
@font-face {
font-family: 'metroSysIcons';
src: url('../fonts/metroSysIcons.woff') format('woff'),
url('../fonts/metroSysIcons.ttf') format('truetype'),
url('../fonts/metroSysIcons.svg#metroSysIcons') format('svg');
font-weight: normal;
font-style: normal;
}
我使用 Assetic 的命令 assets:install 将资产安装到我的 Web 文件夹中。资产实际上已被复制,甚至是字体。
问题
当我访问网页时,我的 css 已加载,但我的字体没有。当我查看开发人员控制台(Chrome 中的 F12)时,我可以看到加载字体导致 404。请求的 URL 是:
http://sub.domain.lc/fonts/metroSysIcons.woff
当我在浏览器中输入这个地址时,我得到以下信息:
No route found for "GET /fonts/metroSysIcons.woff"
404 Not Found - NotFoundHttpException
1 linked Exception:
ResourceNotFoundException »
对于所有其他资产,这都有效:
// This loads the correct bootstrap file:
http://sub.domain.lc/css/afd9510_metro-bootstrap_1.css
所以基本上每个资产都可以这样找到,但不是我的字体文件(woff 和 tff 都是)。
问题
我的问题引发了多个问题:
- 为什么 Symfony 试图通过 app.php,使用路由找到我的字体?
- 所有资产都是通过路由找到的吗?
- 如果是这样:为什么字体文件的路径没有被解析?
- 如何加载我的字体文件?
[编辑]
我试过像这样使用 css 重写过滤器:
{% stylesheets filter='cssrewrite'
'bundles/manager/lib/metro-ui/css/metro-bootstrap.css'
%}
我仍然收到 404,但现在浏览器正在尝试从此位置检索我的字体:
http://sub.domain.lc/bundles/manager/lib/metro-ui/fonts/metroSysIcons.woff
我收到相同的 "No route found for "GET /"...." 错误消息。
您应该将 cssurlrewrite
过滤器与 Assetic 一起使用:
Since Assetic generates new URLs for your assets, any relative paths
inside your CSS files will break. To fix this, make sure to use the
cssrewrite filter with your stylesheets tag. This parses your CSS
files and corrects the paths internally to reflect the new location.
Link 到文档:http://symfony.com/doc/current/cookbook/assetic/asset_management.html
所以在我尝试了所有方法之后,我的 .htaccess 中有些东西阻止了字体文件。
RewriteCond %{HTTP_HOST} ^sub\.domain\.(?:lc|nl)$ [NC]
RewriteRule $(.*) app_dev.php [NC,L,QSA]
我添加了以下条件:
RewriteCond %{HTTP_HOST} ^sub\.domain\.(?:lc|nl)$ [NC]
RewriteCond %{REQUEST_URI} !\.(png|woff|tff)$
RewriteRule $(.*) app_dev.php [NC,L,QSA]
现在我可以加载字体了。
你试过这个吗(有点别名):
{% stylesheets output = 'fonts/metroSysIcons.woff'
'@bundles/manager/lib/fonts/metroSysIcons.woff' %}
{% endstylesheets %}
我不确定 '@bundles/manager/lib/metro-ui/css/metro-bootstrap.css'
行,因为如果您的路径不完全是那一行,它可能会有所不同。
应该是:@bundle_name/path/to/your/file/file.css
别忘了 CSS :
{% stylesheets '@MyBundle/Resources/public/css/my.css' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
我在 Symfony 中加载字体时遇到了一些困难。这是出了什么问题:
情况
我正在使用 Symfony,我正在尝试将 Metro UI CSS 文件添加到一个包中。这些文件位于 Resources/public/lib/metro-ui 文件夹中。
Resources/public/lib/metro-ui
- css
- fonts
- js
- min
我在 twig 文件中有这样的布局:
{% stylesheets
'bundles/manager/lib/metro-ui/css/metro-bootstrap.css'
%}
Metro UI 的 bootstrap 文件包含以下字体:
@font-face {
font-family: 'metroSysIcons';
src: url('../fonts/metroSysIcons.woff') format('woff'),
url('../fonts/metroSysIcons.ttf') format('truetype'),
url('../fonts/metroSysIcons.svg#metroSysIcons') format('svg');
font-weight: normal;
font-style: normal;
}
我使用 Assetic 的命令 assets:install 将资产安装到我的 Web 文件夹中。资产实际上已被复制,甚至是字体。
问题
当我访问网页时,我的 css 已加载,但我的字体没有。当我查看开发人员控制台(Chrome 中的 F12)时,我可以看到加载字体导致 404。请求的 URL 是:
http://sub.domain.lc/fonts/metroSysIcons.woff
当我在浏览器中输入这个地址时,我得到以下信息:
No route found for "GET /fonts/metroSysIcons.woff"
404 Not Found - NotFoundHttpException
1 linked Exception:
ResourceNotFoundException »
对于所有其他资产,这都有效:
// This loads the correct bootstrap file:
http://sub.domain.lc/css/afd9510_metro-bootstrap_1.css
所以基本上每个资产都可以这样找到,但不是我的字体文件(woff 和 tff 都是)。
问题
我的问题引发了多个问题:
- 为什么 Symfony 试图通过 app.php,使用路由找到我的字体?
- 所有资产都是通过路由找到的吗?
- 如果是这样:为什么字体文件的路径没有被解析?
- 如何加载我的字体文件?
[编辑]
我试过像这样使用 css 重写过滤器:
{% stylesheets filter='cssrewrite'
'bundles/manager/lib/metro-ui/css/metro-bootstrap.css'
%}
我仍然收到 404,但现在浏览器正在尝试从此位置检索我的字体:
http://sub.domain.lc/bundles/manager/lib/metro-ui/fonts/metroSysIcons.woff
我收到相同的 "No route found for "GET /"...." 错误消息。
您应该将 cssurlrewrite
过滤器与 Assetic 一起使用:
Since Assetic generates new URLs for your assets, any relative paths inside your CSS files will break. To fix this, make sure to use the cssrewrite filter with your stylesheets tag. This parses your CSS files and corrects the paths internally to reflect the new location.
Link 到文档:http://symfony.com/doc/current/cookbook/assetic/asset_management.html
所以在我尝试了所有方法之后,我的 .htaccess 中有些东西阻止了字体文件。
RewriteCond %{HTTP_HOST} ^sub\.domain\.(?:lc|nl)$ [NC]
RewriteRule $(.*) app_dev.php [NC,L,QSA]
我添加了以下条件:
RewriteCond %{HTTP_HOST} ^sub\.domain\.(?:lc|nl)$ [NC]
RewriteCond %{REQUEST_URI} !\.(png|woff|tff)$
RewriteRule $(.*) app_dev.php [NC,L,QSA]
现在我可以加载字体了。
你试过这个吗(有点别名):
{% stylesheets output = 'fonts/metroSysIcons.woff'
'@bundles/manager/lib/fonts/metroSysIcons.woff' %}
{% endstylesheets %}
我不确定 '@bundles/manager/lib/metro-ui/css/metro-bootstrap.css'
行,因为如果您的路径不完全是那一行,它可能会有所不同。
应该是:@bundle_name/path/to/your/file/file.css
别忘了 CSS :
{% stylesheets '@MyBundle/Resources/public/css/my.css' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}