2012-11-10 155 views
8

我想在我的視圖中添加資產圖像。但如果我加載頁面的圖像將不會加載..Symfony 2資產圖像

配置:

framework: 
    esi:    ~ 
    translator:  { fallback: "%locale%" } 
    secret:   "%secret%" 
    router: 
     resource: "%kernel.root_dir%/config/routing.yml" 
     strict_requirements: "%kernel.debug%" 
    form:   true 
    csrf_protection: true 
    validation:  { enable_annotations: true } 
    templating:  { engines: ['twig'], assets_version: "1.0.0" } 
    default_locale: "%locale%" 
    trust_proxy_headers: false # Whether or not the Request object should trust proxy headers (X_FORWARDED_FOR/HTTP_CLIENT_IP) 
    session:   ~ 

# Twig Configuration 
twig: 
    debug:   "%kernel.debug%" 
    strict_variables: "%kernel.debug%" 

# Assetic Configuration 
assetic: 
    debug:   "%kernel.debug%" 
    read_from:  %kernel.root_dir%/../public_html 
    write_to:  %kernel.root_dir%/../public_html 
    use_controller: false 
    #bundles:  [ ] 
    #java: /usr/bin/java 
    filters: 
     cssrewrite: ~ 
     #closure: 
     # jar: "%kernel.root_dir%/Resources/java/compiler.jar" 
     #yui_css: 
     # jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar" 

index.html.twig

<img src="{{ asset('images/menu-park.gif') }}"> 

要安裝我跑的資產:

php app/console assets:install public_html --env=dev --symlink 

我忘記了什麼嗎?

+1

你在img的src屬性中獲得的url是什麼? – AdrienBrault

+0

回答

21

assets:install public_html命令將src/Acme/DemoBundle/Resources/public/目錄複製到public_html/bundles/acmedemo/目錄中。所以你需要參考那個網址。

例如:您有一個名爲MvParkBundle扎,你已經創建了Resources/public/images目錄內的圖像menu-park.gif,您應使用此代碼裏面樹枝,假設前端控制器裏面public_html太:

<img src="{{ asset('bundles/mvpark/images/menu-park.gif') }}"> 

如果您運行assets:install public_html命令,則一切都應該正確。

+0

但爲什麼--symlink不起作用?它應該是一樣的? –

+0

您也可以使用--symlink,但是您需要將Twig模板中的路徑更改爲「bundles/{bundlename}/images/menu-park.gif」。 –

+2

問題是:'app/console assets:install public_html' works,但'app/console assets:install public_html --symlink' does not work .. but why:S –