4

I'm trying to setting up a site I'm working on at shared hosting and all works fine but FontAwesome icons since Symfony does not find them where they should be. I follow this steps to move the site to production shared hosting:

  • Publish assets as hard copy since SH doesn't allow symlink so I run this command assets:install
  • Publish assets handled by Assetic by running this commands: assetic:dump (dev) and assetic:dump --env=prod (prod)

But it's not working since I'm getting this errors at Firebug all the time:

"NetworkError: 404 Not Found - http://tanane.com/bundles/backend/img/mybg.png"
"NetworkError: 404 Not Found - http://tanane.com/bundles/backend/fonts/fontawesome-webfont.woff?v=4.1.0"
"NetworkError: 404 Not Found - http://tanane.com/bundles/backend/fonts/fontawesome-webfont.ttf?v=4.1.0"

At localhost, following the same steps things works fine so I don't know if is a permission problem or another problem.

This is how I define the assets at base.html.twig:

  {% block stylesheets %}
      {% stylesheets
              'bundles/template/css/bootstrap.min.css'
              'bundles/template/css/bootstrap-theme.min.css'
              'bundles/template/css/font-awesome.min.css'
              'bundles/template/css/select2.css'
              'bundles/template/css/select2-bootstrap.css'
              'bundles/template/css/bootstrapValidator.min.css'
              'bundles/template/css/datepicker.css'
              'bundles/template/css/datepicker3.css'
              'bundles/template/css/tanane.css'
         filter='cssrewrite'
      %}
      <link rel="stylesheet" href="{{ asset_url }}" />
     {% endstylesheets %}
  {% endblock %}

I did a research and found a lot of topics around this problem as for example this one also I found this interesting one but have my doubts around the second one.

Can any give me a help on this? I'm stucked

Installed SpBowerBundle + FkrCssURLRewriteBundle

I have installed and configured both bundles but even after that I'm still having problems with images in this case, just in Select2 library.

This is the bower.json file content:

{
    "name": "TemplateBundle",
    "dependencies": {
        "bootstrap": "latest",
        "bootstrap-datepicker": "latest",
        "bootstrap-growl": "latest",
        "bootstrapvalidator": "latest",
        "jquery": "1.11.*",
        "jquery-migrate": "latest",
        "pwstrength-bootstrap": "latest",
        "select2": "latest",
        "font-awesome": "latest"
    }
}

And this are the lines I've added to /app/config/config.yml

#FkrCssURLRewriteBundle 
fkr_css_url_rewrite:
    rewrite_only_if_file_exists: true
    clear_urls: true

# SpBowerBundle
sp_bower:
    install_on_warmup: true
    allow_root: true
    assetic:
        enabled: true
        nest_dependencies: false
        filters:
            packages:
                bootstrap:
                    css:
                        - css_url_rewrite
                font_awesome:
                    css:
                        - css_url_rewrite
    bundles:
        TemplateBundle: ~

This is the error I'm getting now:

"NetworkError: 404 Not Found - http://tanane.dev/select2.png"
"NetworkError: 404 Not Found - http://tanane.dev/select2-spinner.gif"

Why?

Disabled Assetic in SpBowerBundle

I've disabled assetic in SpBowerBundle at /app/config/config.yml:

# SpBowerBundle
sp_bower:
    install_on_warmup: true
    allow_root: true
    bundles:
        TemplateBundle: ~

Since I'm using assetic and also SpBowerBundle to handle libraries dependencies then I rewrite the CSS/JS blocks at base.html.twig as follow:

{% stylesheets
    'bundles/template/components/bootstrap/dist/css/bootstrap.min.css'
    'bundles/template/components/bootstrap/dist/css/bootstrap-theme.min.css'
    'bundles/template/components/font-awesome/css/font-awesome.min.css'
    'bundles/template/components/select2/select2.css'
    'bundles/template/css/select2-bootstrap.css'
    'bundles/template/components/bootstrapvalidator/dist/css/bootstrapValidator.min.css'
    'bundles/template/components/bootstrap-datepicker/css/datepicker.css'
    'bundles/template/components/bootstrap-datepicker/css/datepicker3.css'
    'bundles/template/css/tanane.css'
   filter='css_url_rewrite'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

Then I clear the cache and run the commands assets:install --symlink, assetic:dump and assetic:dump --env=prod and still not seeing images and also FontAwesome fonts:

Failed to load resource: the server responded with a status of 404 (Not Found) http://tanane.dev/app_dev.php/css/select2.png
Failed to load resource: the server responded with a status of 404 (Not Found) http://tanane.dev/app_dev.php/css/select2-spinner.gif
Failed to load resource: the server responded with a status of 404 (Not Found) http://tanane.dev/app_dev.php/css/select2.png
Failed to load resource: the server responded with a status of 404 (Not Found) http://tanane.dev/app_dev.php/fonts/fontawesome-webfont.woff?v=4.2.0
Failed to load resource: the server responded with a status of 404 (Not Found) http://tanane.dev/app_dev.php/fonts/fontawesome-webfont.ttf?v=4.2.0
Failed to load resource: the server responded with a status of 404 (Not Found) http://tanane.dev/app_dev.php/fonts/fontawesome-webfont.svg?v=4.2.0#fontawesomeregular

I miss something else? What else I can do it in order to fix this annoying issue?

Fixing the disable way

I fixed some mistakes I made in SpBowerBundle configuration and now I have this:

sp_bower:
    install_on_warmup: true
    allow_root: true
    assetic:
        enabled: false
        nest_dependencies: false
    bundles:
        TemplateBundle: ~

But images, managed by SpBowerBundle still not showing, see the attached image:

enter image description here

I have assetic enabled in my config.yml:

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:
      - FrontendBundle
      - BackendBundle
      - ProductBundle
      - CommonBundle
      - UserBundle
      - TemplateBundle

Should I disable it and remove all those bundles from there?

Another test

Following @lenybenard suggestions I did this:

    {% block stylesheets %}
        {% stylesheets filter='css_url_rewrite'
            'bundles/template/components/font-awesome/css/font-awesome.min.css'
            'bundles/template/components/bootstrap/dist/css/bootstrap.min.css'
            'bundles/template/components/bootstrap/dist/css/bootstrap-theme.min.css'
           filter='cssrewrite'
        %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}

        {% stylesheets
            'bundles/template/components/select2/select2.css'
            'bundles/template/css/select2-bootstrap.css'
            'bundles/template/components/bootstrapvalidator/dist/css/bootstrapValidator.min.css'
            'bundles/template/components/bootstrap-datepicker/css/datepicker.css'
            'bundles/template/components/bootstrap-datepicker/css/datepicker3.css'
            'bundles/template/css/tanane.css'
           filter='cssrewrite'
           filter='css_url_rewrite'
        %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}

Repeat the same process once again:

  • Clear cache cache:clear & cache:warmup and also rm -rf /var/cache & rm -rf /var/logs just in case
  • From Symofony2 shell: assets:install --symlink & assetic:dump & assetic:dump --env=prod

Result: in DEV all is fine, in PROD all is wrong

Community
  • 1
  • 1
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Try `app/console assets:install path` if you don't use default `web` path. – Lkopo Sep 04 '14 at 15:20
  • @Mr.Smith I'm using default web path – ReynierPM Sep 04 '14 at 15:26
  • Can you please post the part of your twig template where you define these assets and create the link tags to include them – Tom Corrigan Sep 04 '14 at 15:32
  • @TomCorrigan added to the main post, take a look – ReynierPM Sep 04 '14 at 15:35
  • @TomCorrigan can you take a look to info I added to the main post? I'm still having issues around this – ReynierPM Sep 06 '14 at 18:45
  • I took a look at your site when you posted the link ( I think you have since taken it down) and it seems your issue now is with the webserver configuration. You need to set the base path you serve the application from as the `web/` subdirectory. You were serving out of the project base path and hence I could browse your vendors files etc. I think this might have been stuffing up the asset paths assetic was choosing. – Tom Corrigan Sep 16 '14 at 10:07

5 Answers5

4

This is the result of a rather unfortunate bug in assetic. See this github description for further details.

The solution I have settled on is to use https://github.com/fkrauthan/FkrCssURLRewriteBundle

It is the only approach of the many I have tried that works in every case.

Tom Corrigan
  • 394
  • 1
  • 11
  • One question, I should rewrite the routes from `'bundles/template/css/bootstrap.min.css'` to `'@TemplateBundle/Resources/public/css/bootstrap.min.css'`? Should I use minified version or normal version? What about if I like to handle assets with bower and [this][https://github.com/Spea/SpBowerBundle] bundle? – ReynierPM Sep 04 '14 at 16:10
  • @ReynierPM Using the alternative syntax to might solve your problems in this case but it doesn't always work. The `FkrCssURLRewriteBundle` does. I have used SpBowerBundle in conjunction with FkrCssURLRewriteBundle flawlessly. – Tom Corrigan Sep 04 '14 at 16:14
  • So what's you recommendation around syntax to follow? The one I have or the one you use on bundle docs? – ReynierPM Sep 04 '14 at 16:15
  • I think you are now having a problem because you haven't configured the `css_url_rewrite` filter for the select2 package in the bower bundle. Personally I prefer to disable the assetic integration with SpBowerBundle and just use it to run bower to install assets. I then reference the files in my templates like so: `bundles/namespace/components/select2/select2.css` – Tom Corrigan Sep 04 '14 at 23:54
  • I've disabled assetic as you suggested and error still, any other advice? – ReynierPM Sep 05 '14 at 02:12
  • Assetic is enabled by default in the `SpBowerBundle` so you actually need to disable it in your `config.yml` by putting `assetic: false` under `sp_bower` – Tom Corrigan Sep 05 '14 at 02:23
  • I added this `assetic: enabled: false nest_dependencies: false` to `SpBowerBundle` repeat the whole process and nothing images still lost, bust just only the one manages by Bower dependencies, take a look at the image I leave in main post – ReynierPM Sep 05 '14 at 02:31
  • This might sound crazy but the final step is probably going to be adding the built in `cssrewrite` filter as well. – Tom Corrigan Sep 05 '14 at 02:38
  • crazy or not apparently that solves the issue. The other thing I do was to open the same page in Chrome incognito window for work without any kind of cache and it's working now, so thanks for your time. Have a great day – ReynierPM Sep 05 '14 at 02:48
3

i got same problem, and this is worked

i use https://github.com/fkrauthan/FkrCssURLRewriteBundle and then in #app/config/config.yml add this

font-awesome-otf:
        inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/FontAwesome.otf'
        output: 'fonts/FontAwesome.otf'
font-awesome-eot:
        inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.eot'
        output: 'fonts/fontawesome-webfont.eot'
font-awesome-svg:
        inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.svg'
        output: 'fonts/fontawesome-webfont.svg'
font-awesome-ttf:
        inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.ttf'
        output: 'fonts/fontawesome-webfont.ttf'
font-awesome-woff:
        inputs: '%kernel.root_dir%/../vendor/fortawesome/font-awesome/fonts/fontawesome-webfont.woff'
        output: 'fonts/fontawesome-webfont.woff'

I Would like thanks to : http://www.maraumax.fr

enhaka
  • 85
  • 8
1

Actually, this is quite logical, in dev environment, it works because assetic creates as many file there is resources.

But when you're in production mode, each assetic block compiles all your resources in one single file by concatenating all your resource files.

The problem is that in css, an @import must be in the top of the file... and here, in prod, your font-awesome import is inside a file and is not read by your browser.

To fix your problem, you could do this :

Import first the stylesheet using @import :

{% stylesheets filter='css_url_rewrite'
    'bundles/template/components/font-awesome/css/font-awesome.min.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

Then, import the rest

{% stylesheets
    'bundles/template/components/bootstrap/dist/css/bootstrap.min.css'
    'bundles/template/components/bootstrap/dist/css/bootstrap-theme.min.css'
    'bundles/template/components/select2/select2.css'
    'bundles/template/css/select2-bootstrap.css'
    'bundles/template/components/bootstrapvalidator/dist/css/bootstrapValidator.min.css'
    'bundles/template/components/bootstrap-datepicker/css/datepicker.css'
    'bundles/template/components/bootstrap-datepicker/css/datepicker3.css'
    'bundles/template/css/tanane.css'
   filter='css_url_rewrite'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
lenybernard
  • 2,399
  • 22
  • 22
  • You're right my problem is fixed at `dev` but in `prod` still how can I fix that? – ReynierPM Sep 05 '14 at 12:36
  • Thanks, that fixed the issue at prod level – ReynierPM Sep 05 '14 at 17:56
  • I tough the problem was fixed but don't and I don't know what else to do. Take a look [here](http://tanane.com/tanane/web/app_dev.php/order/natural) which is `dev` and the same [here](http://tanane.com/tanane/web/order/natural) but in `prod`, there is another idea coming to you? – ReynierPM Sep 06 '14 at 18:41
  • can you take a look to info I added to the main post? I'm still having issues around this – ReynierPM Sep 06 '14 at 18:45
0

I've been dealing with this for a while and came up with a new solution. Improving @lenybernard's answer a little bit, here is my solution:

Since the @import is not located in the font-awesome-min.css file, it didn't work for my case. I was using a theme and found out that the style sheet related to the theme had the import annotation. Also, the theme was requiring Bootstrap to be loaded before its own CSS file, so moving the theme.css file to top of the list, or separating it as mentioned in the previous answer broke the theme completely. So, an ultimate solution for this is to find the line with the @import tag and put it in the first line of the first file in your assetic list, and remove it from the file it is referred. For example:

'bundles/foo/bootstrap.css'
'bundles/foo/custom_theme.css' <- if your @import is here
'bundles/foo/font-awesome.css'

...

'bundles/foo/bootstrap.css' <- put it here
'bundles/foo/custom_theme.css' <- and remove it from this one
'bundles/foo/font-awesome.css'

Another solution is to create a new CSS file and name it however you want, put your @import line on this file, and put this file on top of the list. For example:

'bundles/foo/font-awesome-fix.css' <- put @import here
'bundles/foo/bootstrap.css'
'bundles/foo/custom_theme.css' <- and remove it from this one
'bundles/foo/font-awesome.css'
honk
  • 9,137
  • 11
  • 75
  • 83
afkplus
  • 125
  • 3
  • 11
0

I had a similar problem while trying to use fonts with an Ez Publish setup. The fonts were correctly located in the web/font/ directory and the .less was correctly compiled to point to that directory. Yet, I was getting 404 errors while trying to pull the font files.

The problem turned out to be an incorrectly configured virtual host. The config file had this line:

RewriteRule ^/(css|js)/.*\.(css|js) - [L]

Which effectively states 'serve any file from the css or js folder as long as the extension is .js or .css'. I had to modify it to

RewriteRule ^/(css|js|font)/.*\.w* - [L]

to allow for the font folder and any extension. Without this Symfony and Ez Publish were trying to route the url to a dynamic content.

Twifty
  • 3,267
  • 1
  • 29
  • 54