Symfony Assetic featurejust seem not to work - symfony

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

Related

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.

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

Configure output dir for Assetic in Symfony2

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/'

Symfony 2 + Assetic Runtime Java Error

I'm trying to set my Symfony 2 project to combine and compress a number of javascript files. Below is an example of the tag I'm using to accomplish this:
{% javascripts 'bundles/acmedemo/js/*' output='js/plugins.js' filter='closure' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
I've config to map to the yui compressor jar.
assetic:
debug: %kernel.debug%
use_controller: false
filters:
cssrewrite: ~
closure:
jar: %kernel.root_dir%/java/yuicompressor-2.4.6.jar
yui_css:
jar: %kernel.root_dir%/java/yuicompressor-2.4.6.jar
When I try to bring up the page in the browser it takes a while to load and doesn't include the output file. I tried to run the app/console assetic:dump command and got this error:
[RuntimeException]
If no input file is specified, it defaults to stdin. In this case, the 'type' option is required. Otherwise, the 'type' option is required only if the input. And the windows prompt cuts it off.
Has anyone else encountered this issue?
Is you yuicompressor file actually located in %kernel.root_dir%/java/yuicompressor-2.4.6.jar ? not %kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar ?
Check this link, maybe something will help http://groups.google.com/group/symfony2/browse_thread/thread/dde8b418813bab37/1d4e42a7396f2e0f?lnk=gst&q=yui
Additionaly: the path has to be put in: ""
The config will look like this:
assetic:
debug: %kernel.debug%
use_controller: false
filters:
cssrewrite: ~
closure:
jar: "%kernel.root_dir%/java/yuicompressor-2.4.6.jar"
yui_css:
jar: "%kernel.root_dir%/java/yuicompressor-2.4.6.jar"
The following isn't an error as such but you seem to be configuring the YUI compressor from Yahoo as 'closure'. Google Closure is a different JS compression tool.

The combined file is not being included in production

I'm using uglifyjs and uglifycss with symfony to manage my asset.
I have a strange issue.
Here is my configuration
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
filters:
cssrewrite: ~
uglifyjs2:
# the path to the uglifyjs executable
bin: "%kernel.root_dir%/Resources/node_modules/uglify-js/bin/uglifyjs"
uglifycss:
bin: "%kernel.root_dir%/Resources/node_modules/uglifycss/uglifycss"
And in my view
{% javascripts
'#MyBundle/Resources/public/js/global.js'
'#MyBundle/Resources/public/utilis/*.js'
'#MyBundle/Resources/public/utilis/*/*.js'
'#MyBundle/Resources/public/js/*.js'
'#MyBundle/Resources/public/js/*/*.js'
filter='?uglifyjs2' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
With this I expect to have only one file minified included in my page.
On my development machine (Windows 7, php 5.4, symfony 2.7) everything is working fine.
But on production environment (debian 7, php5.4, symfony 2.7), all individual file is being included in the page.
<script src="/js/e434957_part_14_Store_1.js"></script>
<script src="/js/e434957_part_15_CrudPanel_2.js"></script>
<script src="/js/e434957_part_15_Form_3.js"></script>
<script src="/js/e434957_part_15_Grid_4.js"></script>
Can somebody help me understand what is going on?
When you dump the assets, make sure you add --env=prod to the assetic:dump command.
You can also force files combining with combine=true parameter. But it should be set on true automatically on production env.

Resources