This is a bit frustrating. According to Symfony best practices, a bundle's web assets (images, css, js) should be placed in src/vendor/path/to/bundle/Resources/public. Running app/console assets:install copies the contents of that public folder to web/bundles/bundlename. In the official documentation, Twig templates are shown to grab these assets with code like:
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{{ asset('/css/styles.css') }}
{% endblock %}
When I try it, instead of magically grabbing my assets from web/bundles/bundlename/css/styles.css, it instead just goes for web/css/styles.css. Is this expected behavior? The official documentation is less than clear about this.
In order to try to combat this issue, I tried embracing assetic's ability to dynamically serve assets. I tried:
{% stylesheets '#mybundle/Resources/public/css/*' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
But got the following exception:
An exception has been thrown during the compilation of a template ("You must add mybundle to the assetic.bundle config to use the {% stylesheets %} tag in mybundle:Home:index.html.twig.") in "/home/kevin/www/src/mybundle/Resources/views/Home/index.html.twig
The official Symfony documentation makes it appear that it should work out of the box with no configuration necessary.
So, TLDR:
Am I not understanding how assets should be loaded?
How do I address the assetic exception?
To fix the assetic exception you need to configure your bundle in your config.yml like this:
assetic:
bundles: [ MyAwesomeBundle ]
The {{ asset(...) }} twig function will serve files relative from your web root.
You use app/console assets:install to install static assets within your web root and you can later easily point to them using the asset function like this {{ asset('/mybundle/css/site.css') }}
Related
Im learning Symfony and trying to set up a boilerplate app in Symfony 4
This Symfony document describes how to include assets in your page, namely using the asset package like so..
<img src="{{ asset('images/logo.png') }}" alt="Symfony!" />
<link href="{{ asset('css/blog.css') }}" rel="stylesheet" />
I have installed this package and trying to link to a css file in my /public directory.
I know Symfony recommends placing assets in an /assets folder at the root, but I would like to avoid this if possible. It makes more sense to me to place assets in the public directory.
My Twig base template is as follows:
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}
<link href="{{ asset('public/css/main.css') }}" rel="stylesheet" />
{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
<footer>footer</footer>
</html>
Problem
When I load the route/page/template, the linked css is rendered as a simple link from the root (as if Im linking it as <link href="public/css/main.css" rel="stylesheet" /> - there is of course no route set for public/* and this returns 404/Not Found.
Its as if the asset() function is not even used.
Ive tried
Restarting my local Symfony server
moving the asset to an assets folder at the root at adjusting the link accordingly
verifying that the asset package was installed (it is)
googling this issue (nothing for Symfony 4)
Questions
How can I let Symfony know that the public/* path is a filesystem path to assets, NOT a URL route, and include my assets successfully?
Is there such a feature to set a default location for assets other than the recommended /assets folder at the root?
Paths in the asset function should be relative to your public directory, not the project root.
So, pretending your server's docroot is public_html, and an asset is in public_html/css/main.css, the asset call would be {{ asset('css/main.css') }}.
Additional note: The asset function doesn't give you the ability to put assets outside the public directory. They must be in the public directory.
Firstly maybe you have to add the asset functionality to Symfony:
composer require symfony/asset
Then you should to use in Twig temlate the relative path to your public directory:
<link href="{{ asset('css/style.css') }}" rel="stylesheet" />
for the public/css/style.css style file.
BTW it is also works for Symfony 5.
Check if webpack is watching files ... (yarn watch) After hours of searching for an answer one of my project team members asked if I had that running ={ Worked immediately after that.
I installed Symfony 3.1.3 and yesterday I lost whole day to figure out why symfony's not loading css files, I'm using Assetic bundle, I followed steps from documentation, I also watched videos on youtube, dumped assets, cleared cache but still nothing..
So, I have this code in base twig:
{% block stylesheets %}
{% stylesheets 'bundles/app/css/*' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endblock %}
I also tried to specify concrete file path but 404 not found.
any ideas?
Usually, after updating the cache, you will want to install the assets too:
In Symfony 3:
php bin/console assets:install web --symlink
In Symfony 2:
php app/console assets:install web --symlink
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:
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
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.
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
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 %}
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'
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.
For some reason I can't load the css files.
Here is the structure of the bundle that I use
BD
WebsiteBundle
public
css
And here is how I try to load the css files
{% stylesheets 'bundles/bdwebsite/css/*' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
I did assetic:dump
What I'm doing wrong?
You referred to bundles/bdwebsite/css/* in your stylesheets tag.
This way assetic looks for all css files in web/bundles/bdwebsite/css (a folder that currently does not exist) and not in src/BD/WebsiteBundle/public/css.
In order to have your files in the right place before executing assetic:dump...
... use app/console assets:install web which will copy them to the web folder...
... or my recommendation app/console assets:install web --symlink for symlinks.
Assetic doesn't work very wells with this tag:
{% stylesheets output='/style/app.css'
'#VendorBundle/Resources/style/main.css'
%}
<link rel="stylesheet" href="{{ asset_url }}"/>
{% endstylesheets %}
If I use it, it's like Assetic doesn't know what to do with this. In config_dev use_controller is true. I can't find what I'm doing wrong. I just get this:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "_assetic_aca6c7a_0" as such route does not exist.") in "VendorBundle:Section:template.html.twig".
Maybe some cache issue? I don't know what I can try...
I had same problem. In my case clearing app/cache and app/logs solved the issue.
Just add the name of your bundle in the config.yml file, so it can have the rights to create files.
assetic:
bundles: [ VendorBundle ]
I had a similar problem, solved by adding a route in routing_dev.php
_assetic:
resource: .
type: assetic