简体   繁体   中英

Symfony assetic doesn't work

Here are my files configured to make every thing functionnal (but it's not ). I did a cache clear, a php app/console assests:intall web , nothing new. Always getting this error message: Cannot load resource "." .

app/config.php

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

app/config_dev.php

assetic:
  use_controller: false

app/routing_dev.php

_assetic:
  resource: .
  type:     assetic

src/Acme/TestBundle/Resources/views/Default/index.html.twig

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

src/Acme/TestBundle/Resources/public/js/main.js

console.log('hello');

Does anyone know if I'm missing something in the config or in my twig files to make the route finally findable :P ? Thank you guys.

try to configure like this:

1- Put your resources in the "public" folder, is found in:

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

2- In some projects I use Yuicompressor for Assetic, (Yuicompressor 2.4.7 works well on windows platform) and put the yuicompressor-2.4.7.jar in:

app/Resources/java/yuicompressor-2.4.7.jar

Important: yuicompressors requires java runtime environment 1.7, as usal it is installed in the C:\\Program Files (x86)\\Java\\jre7\\bin (Win64Bits)

3- Setting the 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- Using the stylesheets and javascripts blocks in a templete twig

for your css files you can use:

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

for your javascripts file you can use:

{% 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- Running commands 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- Check files created

if you go to the web folder in your symfony project, you can see the next files:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM