2015-04-21 31 views
3

我在Symfony中加載字體時感到很困難。這裏是什麼錯誤:在Symfony中找不到字體文件的路由

情況

我使用Symfony和我想的Metro UI的CSS文件添加到包。這些文件存在於Resources/public/lib/metro-ui文件夾中。

Resources/public/lib/metro-ui 
    - css 
    - fonts 
    - js 
    - min 

我有一個樹枝文件這樣的佈局:

{% stylesheets 
    'bundles/manager/lib/metro-ui/css/metro-bootstrap.css' 
%} 

的Metro UI的引導文件包含以下字體:

@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的命令資產:install安裝資產放入我的網頁文件夾。實際上資產已被複制,甚至是字體。

問題

當我去我的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 

我得到相同的「找不到路由」GET /「.....」erro r消息。

+0

檢查'web'文件夾結構,並檢查是否'網/包/經理/ lib目錄/地鐵-ui/fonts/metroSysIcons.woff'是否存在,或者是否存在差異 – Jean

+0

它確實存在並且沒有區別。 –

+0

你正在運行'assets:install'命令嗎? –

回答

0

所以之後我都試過了,有東西在我的.htaccess阻止字體文件。

RewriteCond %{HTTP_HOST} ^sub\.domain\.(?:lc|nl)$ [NC] 
RewriteRule $(.*) app_dev.php$1 [NC,L,QSA] 

我增加了以下條件:

RewriteCond %{HTTP_HOST} ^sub\.domain\.(?:lc|nl)$ [NC] 
RewriteCond %{REQUEST_URI} !\.(png|woff|tff)$ 
RewriteRule $(.*) app_dev.php$1 [NC,L,QSA] 

現在我可以加載字體。

1

您應該使用cssurlrewrite濾波器Assetic:

由於Assetic爲您的資產產生新的網址,你的CSS文件中的任何相對路徑 將打破。爲了解決這個問題,請確保在您的樣式表標籤中使用cssrewrite過濾器。這將解析您的CSS 文件並在內部更正路徑以反映新位置。

鏈接到文檔:http://symfony.com/doc/current/cookbook/assetic/asset_management.html

+0

感謝您的回答。我試過這個,但它也導致了404。我更新了我的問題。 –

0

您是否嘗試過這一點(它種別名名稱):

{% 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 %}