Symfony2资产未正确加载

时间:2014-01-09 13:50:35

标签: php symfony twig assets assetic

tl;底部是博士 我想将Assetic文件(css,js,images,等等)的目录设置为:

/web/assets/{css,js,images}/

这将是我的::base.html.twig文件中使用的主目录。

另外,我将使用/web/assets/bootstrap/{css,js,images}/来处理引导程序文件,这实际上正在运行,我正在使用braincrafted/bootstrap-bundle并且它有一个output: bootstrap参数,这很好。

然后,我正在使用SonataAdminBundle自定义模板,我希望将theese模板资产存储到/web/assets/admin/{css,js,images}/

这是我的config.yml文件

framework:
    #esi:             ~
    translator:      ~
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        packages:
            assetic:
                base_urls: '/assets'
            admin:
                base_urls: '/assets/admin'
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
    fragments:       ~
    http_method_override: true

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

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ "WebDificilBundle"]
    #java: /usr/bin/java
    #read_from:      %kernel.root_dir%/Resources/views/
    write_to:       %kernel.root_dir%/../web/assets/
    filters:
        cssrewrite: ~
        less:
            node: /usr/local/bin/node
            node_paths: [/usr/local/lib/node_modules]
            apply_to: "\.less$"
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

braincrafted_bootstrap:
    less_filter: less
    output_dir: bootstrap
    assets_dir: %kernel.root_dir%/../vendor/twbs/bootstrap
    jquery_path: %kernel.root_dir%/../vendor/jquery/jquery/jquery-1.10.2.js
    auto_configure:
        assetic: true
        twig: true
        knp_menu: true
        knp_paginator: true
    customize:
        variables_file: %kernel.root_dir%/../vendor/twbs/bootstrap/less/variables.less
        bootstrap_output: %kernel.root_dir%/Resources/less/bootstrap.less
        bootstrap_template: BraincraftedBootstrapBundle:Bootstrap:bootstrap.less.twig

这就是我所拥有的,例如::base.html.twig

{% block stylesheets %}
    <!-- Bootstrap -->
    <link href="{{ asset('assets/bootstrap/css/bootstrap.css') }}" rel="stylesheet" media="screen">

    {% stylesheets filter='cssrewrite' package='assetic' 'css/style.css' %}
        <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}

{% endblock %}

{% block javascripts %}
    <script src="https://code.jquery.com/jquery.js"></script>
    <!-- Include all JavaScripts, compiled by Assetic -->
    <script src="{{ asset('assets/bootstrap/js/bootstrap.js') }}"></script>

    <script type="text/javascript">
    <!--
    window.base_url = '{{ path('decision') }}';
    //-->
    </script>
    {% include 'BraincraftedBootstrapBundle::ie8-support.html.twig' %}
{% endblock javascripts %}

默认情况下,这是/web文件夹

robhunter@debianmoi:~/public_html/dificildecision/web$ ls -atls
total 52K
4,0K drwxr-xr-x 3 robhunter robhunter 4,0K ene  9 14:41 ./
4,0K drwxr-xr-x 9 robhunter robhunter 4,0K ene  9 14:14 ../
4,0K -rw-r--r-- 1 robhunter robhunter 2,6K ene  9 12:11 .htaccess
8,0K -rw-r--r-- 1 robhunter robhunter 5,7K ene  9 10:07 config.php
4,0K drwxr-xr-x 2 robhunter robhunter 4,0K dic 28 01:46 fonts/
4,0K -rw-r--r-- 1 robhunter robhunter 1,2K dic 26 22:29 app_dev.php
 12K -rw-r--r-- 1 robhunter robhunter  11K dic 26 22:29 apple-touch-icon.png
4,0K -rw-r--r-- 1 robhunter robhunter  960 dic 26 22:29 app.php
4,0K -rw-r--r-- 1 robhunter robhunter 1,2K dic 26 22:29 favicon.ico
4,0K -rw-r--r-- 1 robhunter robhunter  106 dic 26 22:29 robots.txt

然后我运行这个命令 php app/console cache:clear --env=prod --no-debug | php app/console assets:install | php app/console assetic:dump

我有这个输出

Dumping all dev assets.
Debug mode is on.

14:43:42 [dir+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/css
14:43:42 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/css/bootstrap.css
14:43:43 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/css/bootstrap_bootstrap_1.css
14:43:44 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/css/bootstrap_form_2.css
14:43:45 [dir+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_transition_1.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_alert_2.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_button_3.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_carousel_4.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_collapse_5.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_dropdown_6.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_modal_7.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_tooltip_8.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_popover_9.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_scrollspy_10.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_tab_11.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_affix_12.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/bootstrap_bc-bootstrap-collection_13.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/jquery.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/bootstrap/js/jquery_jquery-1.10.2_1.js
14:43:45 [dir+] /home/robhunter/public_html/dificildecision/app/../web/assets/js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/js/71cfa0b.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/js/71cfa0b_dificildecision_1.js
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/js/71cfa0b_core_2.js
14:43:45 [dir+] /home/robhunter/public_html/dificildecision/app/../web/assets/css
14:43:45 [file+] /home/robhunter/public_html/dificildecision/app/../web/assets/css/64661ba.css



  [RuntimeException]                                                                                      
  The source file "/home/robhunter/public_html/dificildecision/app/../web/css/style.css" does not exist.  



assetic:dump [--watch] [--force] [--period="..."] [write_to]

为什么Symfony2会抛出此错误?它正确地创建了bootstrap东西,它创建了/web/assets{css,js,images} | /web/bundles | /web/assets/bootstrap/{css,js,images}/所以它没关系(实际上,不是/web/assets/admin/因为我现在没有使用thoose twig文件,但是,没有style.css文件是主要的,它在样式表中的::base.html.twig文件中定义...

tl; dr 我需要知道如何在config.yml文件中正确设置资产配置,以及如何在twig文件中设置内容,让所有内容在我想要的文件夹中运行,当然,当用户访问prod env时,如果他们看到来源我不希望看到whatever.com/bundles/mybundle/css/foo.css我只想whatever.com/assets/foo.css

1 个答案:

答案 0 :(得分:0)

{% stylesheets filter='cssrewrite' package='assetic' **'css/style.css'** %}


 <link rel="stylesheet" href="{{ asset_url }}" />

{% endstylesheets %}

使用此指令,您可以将所有css转储到css文件夹

中的style.css文件中