Configure output dir for Assetic in Symfony2 - symfony

I'd like to globally configure the output dir of where assetic dumps my JS files. Currently, they always go to web/js/*. I want to change this to web/js/compiled/*.
It's possible to specify this at a per-file level: http://symfony.com/doc/2.0/cookbook/assetic/asset_management.html#dumping-asset-files
Can't seem to find a way to set this globally across my Symfony app. Any config parameter I'm missing?
UPDATE
Found an assetic config parameter called write_to. Setting this in config.yml causes the command line assetic:dump to dump files to the new dir, but within twig files the asset_url var still points to the original path.

You should use the property write_to.
in my configuration for exemple I use
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: %kernel.debug%
read_from: %kernel.root_dir%/Resources/views/
write_to: %kernel.root_dir%/../web/static/
Your ouput string start where ends write_to
for exemple
{% javascripts filter="closure" output='js/main.js'
...
{% stylesheets filter='compass,?cssrewrite'
'default/static/sass/screen.scss'
output='css/screen.css'
%}
both will placed respectively in /web/static/js/main.js
and /web/static/css/screen.css
assets_base_urls is used to specify base URL's to be used for assets referenced from http and ssl (https) pages.
!! assets_base_urls is also used by {% images %} as the root before output value, but {% images %} doesn't consider write_to when rendering html (only when dumping) so better not using write_to and rely only on output value. More about it in my other post on stackoverflow and in this post on AsseticBundle's github.

You can set the asset path ( assets_base_urls ) for twig to a static path, instead of using the relative path. In your config.yml file, it would look similar to this:
framework:
templating:
engines: ['twig']
assets_base_urls:
http: [http://path.to-cdn.com]
This will effect asset_url from assetic as well as twig's asset() method. The latter may or may not be desired.

This GitHub issue comment helped me with this issue.
While in dev, your assets will still go thru the controller but in production, the URLs will be as you desire.
Example config.yml:
assetic:
write-to: %kernel.root_dir%/../web/assets
...
framework:
...
templating:
engines: ['twig']
packages:
assetic:
base_urls: '/assets'
Example in your template:
{% block javascripts %}
{% javascripts '#jquery' '#bootstrap_js' '#backbone' '#handlebars' combine=true package='assetic' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
Notice that you have to add the package='assetic' attribute in the java scripts tag. This is a good compromise IMO because it won't break assets from other bundles as kmfk's solution will.

Just a quick note on this. If you're using assets_base_urls, to specify a relative base URL, this only works prior to Symfony 2.7, due to the introduction of the new assets component in that version. Further information on how to change this is available at http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component , but the long and short of it is that:
framework:
templating:
assets_base_urls:
http: ['/some-relative-url']
ssl: ['/some-relative-url']
becomes:
framework:
assets:
base_path: /some-relative-url

Try this commande $ app/console --env=prod assetic:dump web/
you have juste to change the url you want raher than 'web/'

Related

Asset_url in production environment Symfony 3

I have this doubt about URLs of assetics who I generated.
On the view, this was what I did:
{% block stylesheets %}
{% stylesheets
'assets/vendor/bootstrap/dist/css/bootstrap.min.css'
'assets/vendor/font-awesome/css/font-awesome.min.css'
'assets/vendor/dist/css/*.css'
output = 'bundles/compiled/css/app.css' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endblock %}
{% javascripts
'assets/vendor/jquery/dist/jquery.min.js'
'assets/vendor/bootstrap/dist/js/bootstrap.min.js'
'assets/vendor/dist/js/wow.min.js'
'assets/vendor/dist/js/slider.js'
'assets/vendor/dist/js/jquery.fancybox.js'
'assets/vendor/dist/js/owl.carousel.min.js'
'assets/vendor/dist/js/main.js'
'assets/vendor/dist/js/modernizr-2.6.2.min.js'
output = 'bundles/compiled/js/app.js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
In dev environment, the app doesn't call these outputs links who assetic:dump generate, which is:
bundles/compiled/css/app.css
and
bundles/compiled/js/app.js/
both after /web folder.
In prod, I can't reach these locations. I get and 404 on
http://link.com/bundles/compiled/css/app.css for example.
So, how can I configure my config.yml for reach that path in prod environment?
This is what I have:
assetic:
debug: '%kernel.debug%'
use_controller: '%kernel.debug%'
filters:
cssrewrite: ~
**Edit:**I figured out the problem. My assetic's config was alright, the problem was in VirtualHosts file (modrewrite line, to be specific), which was only accepting ".php" extension files and web/ folder permissions.
That's the whole point of using AsseticBundle! When you are in prod mode, you need to dump the asset files. Have a look here.
php bin/console cache:clear --env=prod //clear the cache for prod environment
php bin/console assetic:dump --env=prod --no-debug
The result will be the concatenation of all your css stylesheets, in one single result file (bundles/compiled/css/app.css), and the concatenation of all your js scripts in one single result file (bundles/compiled/js/app.js), for the reason of reducing the requests to the server.
At this point, you won't be able to see, in the source of the page, all the css + js files, except those two from your compiled/ directory.
When you are in dev mode, the default behavior of AsseticBundle is to generate all your css + js files, for debugging purposes. That is, to help you debug your code easily. So at this point you will be able to see all you css+js files, but not the two files from compiled/ directory.
And pay attention, as cssrewrite must be a child of filters. See here the correction you need.

Assetic and domain folder

I'm using Assetic to manage assets in my Symfony2 project. It worked well before i made my application accessible with a domain folder.
before : myapplication.local // Assetic works
now : mydomain.local/myapplication // Assetic doesn't work
The requested css files are called but the filter cssrewrite writes a wrong path for the ressources.
Error :
NetworkError: 404 Not Found - http://www.mydomain.local/Resources/public/images/menu/nav-bg-1.png
The expected URL should looks like http://www.mydomain.local/myapplication/Resources/public/images/menu/nav-bg-1.png
Here is my Assetic Call
{% block stylesheets %}
{% stylesheets
'#Mybundle/Resources/public/css/myfile.css' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endblock %}
I know Assetic say to not use the #-naming when using cssrewrite. But it worked fine without the domain folder, and using "bundles/mybundle/css/myfile.css" does not solve the problem.
If you need anymore information, just ask me ;).
Thank you for your time and you help.
You should use naming without # as said in the documentation. Also you need to dump your assets via assetic every time you change anything with them.
To dump assetic run next command:
php app/console assetic:dump
or
php app/console assetic:dump --env=prod // To generate only files needed for production environment
I managed to fixe this issue by removing the cssrewrite filter and putting all my images, fonts, etc... in web/images or web/fonts, etc... Then in my css, all my path are like "../folder/file"
You can define the assets base URL in your app/config/config*.yml files:
In dev environment: app/config/config_dev.yml
framework:
templating:
engines: ['twig']
assets_base_urls:
http: [http://www.mydomain.local/myapplication/]
In prod environment: app/config/config_prod.yml
framework:
templating:
engines: ['twig']
assets_base_urls:
http: [http://www.example.com/static/]
The cssrewrite filter should remove Resources/public/ from the URL.

Symfony Assetic featurejust seem not to work

Current solutions to this question don't work...
I can't seem to get Symfony assetic to function. I have the following configured in my twig template
{% javascripts
"/js/foundation/foundation.abide.js"
"/js/foundation/foundation.reveal.js"
"/js/edged/jquery-ui.form.js"
"/js/edged/jquery-ui.table.js"
"/js/jquery.autocomplete.js"
"/js/jquery.typewatch.js"
output="/js/combined.js"
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
For same reason in development environment with use_controller = true I get the following error
An exception has been thrown during the rendering of a template
("Unable to generate a URL for the named route "_assetic_476d03f_0" as
such route does not exist.") in admin/view.html.twig at line 314.
When I set use_controller to false and do the whole assetic dump
app\console assetic:dump
The assets are not found, after digging around I noticed that the the _assetic route configurations have been removed from version 2.6 and 2.7, I think as of version 2.4 adding the configuration doesn't resolve any of the issues am facing.
Such a cool feature and I can;t get it working :(
Verify in your config.yml file under Assetic Configuration that you have the bundle registered.
assetic:
debug: "%kernel.debug%"
use_controller: false <-- this should be set to true in config_dev.yml
bundles:
- AppBundle <-- your bundle name

Assetic creates wrong path in template

Im am using Symfony 2.5.3 with Assetic Bundle 2.5.0. Assetic keeps generating wrong urls to generated asset files.
My config_dev.yml looks like this:
assetic:
use_controller: false
write_to: %kernel.root_dir%/../web/dev/dev-env/
When I run php app/console assetic:dump --env=dev the files are correctly written to the above configured directory.
However, in the twig template, the output is like this:
<script src="/js/7cca762_part_4_timer_2-33c923b.js"></script>
<script src="/js/7cca762_part_4_uploader_3-6336104.js"></script>
when it should be
<script src="/dev/dev-env/js/7cca762_part_4_timer_2-33c923b.js"></script>
<script src="/dev/dev-env/js/7cca762_part_4_uploader_3-6336104.js"></script>
The twig template looks like this:
{% javascripts
'#AcmeProjectBundle/Resources/js/*' filter='?uglifyjs2'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Any ideas?
Update:
I just found this Stackoverflow item describing the same problem - also without a clear solution so far.
try out asset base urls
framework:
templating:
assets_base_urls:
http: your.url.com/dev/dev-env

assetic:dump fails on "less" filter

When I try and dump my assetic managed content for a Symphony2 app, I get the following error:
$ php app/console assetic:dump -e prod
Dumping all prod assets.
Debug mode is off.
[InvalidArgumentException]
There is no "less" filter.
However, as far as I can tell I don't use any assets that require the less filter, certainly a grep of all my twig templates doesn't turn anything up.
One of the dependencies I've installed via composer (Twitter's bootstrap) has some .less templates, but I don't reference them in my twig templates, I just point it at the css version. Will assetic still try and dump them? How can I tell it not to?
For reference this is how I include the css in my template
{% stylesheets filter="cssrewrite"
'../vendor/jquery-ui-css/jquery-ui-css/*css'
'../vendor/twitter/bootstrap/docs/assets/css/bootstrap*.css'
%}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}">
{% endstylesheets %}
hm many possible causes ...
1) some third-party bundle adds an assetic collection dependeing on the less filter in a compiler pass
2) there is an apply-to rule like
assetic:
filters:
less:
apply_to: *.less
... in your assetic configuration i.e. app/config/config.yml
3) there is an asset collection using less filter in your configuration
assetic:
assets:
css_character:
inputs:
- "%kernel.root_dir%/../src/Acme/YourBundle/Resources/public/less/*.less"
outputs:
- css/my.css
filters:
- less
4) one of your third-party bundles provides a twig template using assetic's {% stylesheets %} function with the less filter:
{% stylesheets "#AcmeTwitterBundle/Resources/bootstrap/less/*.less" filter="less" %}
{# ... {{ asset_url }} ...#}
{% endstylesheets %}
Now how to find out?
At first check your config files app/config/config.yml and other included ones for assetic entries using the less filter.
The easiest way to find out where the less filter is being used is installing ElaoWebProfilerExtraBundle, clearing your cache and having a look at the "Assetic" tab in the left-side menu of the profiler. You will get an overview of all assetic collections and the filters they use.
Another option - not involving a new bundle although WebProfilerExtraBundle is awesome - is to disable your third-party bundles one by one ( and clear the cache each time ) in app/AppKernel.php try if assetic:dump still throws the exception until you find the bad-boy.
Or dirty: enable the filter though less is probably not installed and see where the next exception is thrown:
assetic:
filters:
less: ~
... all in all i would bet you included the supercool mopa-bootstrap bundle of which almost nobody knows what it's actually doing behind the scenes but it's famous and many people install it because of it's KnpBundle's score.
meaning ... your exception would then be thrown because of the less files included in the templates provided by MopaBootstrapBundle i.e. here.

Resources