Symfony资产并不起作用

时间:2015-11-13 21:53:52

标签: php symfony assetic

以下是我配置的文件,以使每件事都发挥作用(但事实并非如此)。我做了一个清除缓存,php app/console assests:intall web,没什么新东西。始终收到此错误消息:Cannot load resource "."

应用程序/ config.php中

twig:
  paths:
    "%kernel.root_dir%/../src/Acme/TestBundle": AcmeTestBundle
assetic:
  debug:          "%kernel.debug%"
  use_controller: false
  bundles:        [AcmeTestBundle]

应用程序/ config_dev.php

assetic:
  use_controller: false

应用程序/ routing_dev.php

_assetic:
  resource: .
  type:     assetic

的src / Acme公司/ TestBundle /资源/视图/默认/ index.html.twig

{% javascripts '@AcmeTestBundle/Resources/public/js/main.js'  %}
  <script src="{{ asset_url }}"></script>
{% endjavascripts %}

的src / Acme公司/ TestBundle /资源/公共/ JS / main.js

console.log('hello');

有没有人知道我是否在配置或我的树枝文件中遗漏了一些东西,以使路线最终可以找到:P?谢谢你们。

1 个答案:

答案 0 :(得分:1)

尝试像这样配置:

1-将您的资源放在“public”文件夹中,位于:

 YourBundle/Resources/config/public/css
 YourBundle/Resources/config/public/js
 YourBundle/Resources/config/public/images
 YourBundle/Resources/config/public/fonts

2-在某些项目中,我使用Yuicompressor for Assetic,(Yuicompressor 2.4.7在Windows平台上运行良好)并将yuicompressor-2.4.7.jar放入:

app/Resources/java/yuicompressor-2.4.7.jar

重要: yuicompressors需要java运行时环境1.7,因为它安装在C:\ Program Files(x86)\ Java \ jre7 \ bin(Win64Bits)

3-设置config.yml

...
assetic:
    debug:            "%kernel.debug%"
    use_controller:   false
    bundles:          [ ]
    java:             "C:/Program Files (x86)/Java/jre7/bin/java.exe"
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        yui_css:
           jar:   "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
        yui_js:
           jar:   "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
...

4-在模板树枝中使用样式表和javascripts块

您可以使用的css文件:

{% stylesheets      
    'bundles/app/css/styles.css'
    'bundles/app/css/others.css'
    filter='?yui_css, cssrewrite'
    output='css/common-stylesheets.css' %}
    <link href="{{ asset_url }}" rel="stylesheet" />
{% endstylesheets %}

您可以使用的javascripts文件:

{% javascripts           
    '@AppBundle/Resources/public/js/myApp.js'
    '@AppBundle/Resources/public/js/otherFiles.js'

    filter='?yui_js'
    output='js/common-javascripts.js' %}
    <script src="{{ asset_url }}" type="text/javascript"></script>
{% endjavascripts %}

5-运行命令Symfony

app/console cache:clear
app/console cache:clear --env=prod
app/console assets:install web
app/console assetic:dump
app/console assetic:dump --env=prod

6-检查创建的文件

如果你去symfony项目的web文件夹,你可以看到下一个文件:

web/css/common-stylesheets.css
web/javascript/common-javascripts.js