how to reload a javascript asset in Symfony2? - symfony

I have the code below (it's in my twig file) to attach a javascript file specific to this template.
<script type="text/javascript" src="{{ asset("bundles/myappmybundle/js/jobs.js") }}"></script>
It is working fine EXCEPT the fact even if I made changes to the javascript file, somehow my browser (firefox with firebug) is still loading a previous version.
I've tried:
clearing cache via php app/console
rm -rf app/cache/*
Any ideas on how to fix this?
Thanks a lot

It might be in two cases:
Your browser cached this js for you. Try to clear cache of your browser
You installed assets once with hard-copy option (by default). And after you changed files you didn't update your assets with php app/console assets:install.
To make your assets always synced with your bundle's code you can just run in CLI:
php app/console assets:install --symlink
It will create symlink to src/YourBundle/Resources/public folder in the /web/bundles/

Related

Symfony - Assets in dev mode

I am working through symfony tutorials and the documentation, it seems to imply that in dev mode the resources are available without having to install the assets, something like:
<link rel="stylesheet" href="{{ asset("bundles/yodauser/css/login.css") }}" />
I get a 404 error though in dev mode until I actually run the assets:install command.
What am I doing wrong?
Quoting documentation:
http://symfony.com/doc/current/book/templating.html#including-stylesheets-and-javascripts-in-twig
You can also include assets located in your bundles' Resources/public
folder. You will need to run the php bin/console assets:install target
[--symlink] command, which moves (or symlinks) files into the correct
location. (target is by default "web").
<link href="{{ asset('bundles/acmedemo/css/contact.css') }}" rel="stylesheet" />
It should work, in my case sometimes it does sometimes it doeasnt.
Worst case scenario you will have to run command every time you update assets.
404 Error will be trough when the file don't exist on your server!(not found...) (W3 official doc here). The file don't exist in your target folder (or you target the wrong file ^^) ...
have you think to load your ressources in your web/ folder with this command:
# make a hard copy of the assets in web/
$ php app/console assets:install
# if possible, make absolute symlinks in web/ if not, make a hard copy
$ php app/console assets:install --symlink
# if possible, make relative symlinks in web/ if not, make a hard copy
$ php app/console assets:install --symlink --relative
This a new command on SF2.6 and you can find more samples of this command documentation here
you have all the documentation of assetics on the officoal symfony website.
And few idea for the best practise here.
Also, it's better to use the ' character in your asset() function like that :
<link rel="stylesheet" href="{{ asset('bundles/yodauser/css/login.css') }}" />

Symfony2:css, images and assetic

I've put a css file in MyDirectoryBundle/Resources/public/css.
I then linked to it using:
<link href="{{ asset('css/main.css') }}" type="text/css" rel="stylesheet" />
I checked if it worked
short test: body {color: #FF0000;}
Nothing changed.
The parent layout (layout.twig.html) is in app/Resources/views (3 level architecture).
I still don't understand why:
css does not work
why the command: php console assets:install web/ --symlink fails
-> answer: The target directory web does not exist. (it does exist, actually).
I am quite new to symfony2 (2 weeks practice only).
I am working on Windows - just in case this may explain something...
Thank you very much for your help!
If the file first lived in MyDirectoryBundle/Resources/public/css, it'll be copied to web/bundles/mydirectory/css. So you have to include it like: {{ asset('bundles/mydirectory/css/main.css') }}.
You should execute the command from the root of your project. As you're using php console, it seems like you're in the app/ directory when executing this command. In that case, app/web/ does not exists, so the error is completely valid.
As you're on Windows, there is a change that symlink is not allowed/available. I believe it's available for admins only since Windows vista.
As a side tip, if MyDirectoryBundle is your app bundle (a bundle tied to your app and not meant to be reused by other apps), I would recommend to put the CSS file in the web/ directory directly. There is no need to put it in the bundle in such a case (this is only related to bundles that are shared, so the CSS is shared as well).
use php app/console instead of php console

symfony assetic dir. does not exist

I've dumped assets using the folling command:
php app/console assetic:dump
I've got a Runtime exception:
The source file "c:\EasyPHP\data\localweb\projects\symfony\src\LV\IndexBundle/Resources/public/images/" does not exist.
It seems to me strange that:
the / goes to \Resources only, after it is /
the directory images does exist in public
no CSS work. I even tried internal CSS in layout.html.twig. Nothing changes.
OS: Windows 10
Try to remove vendors and re composer install, clear cache directory.
If problem isn't solved after that, please share your config.yml here

How to get assetic to update assets in dev enviroment in symfony2?

I have the issue that my assets, e.g. my sass and javascript, did not recompile on file modification. I installed I accessed them via symlink option and access the dev environment via localhost:8000. I have to manually call /app/console assetic:dump for them to update.
How do I get assetic to watch for file modification?
You can also use
php app/console assetic:dump --watch
If you want to watch for file modifications. This will re-run assetic:dump in the background automatically for you.
And yes dont forget if you use dev then access your site via app_dev.php
And small note... when you use production environment then make sure you execute assetic:dump with --env=prod (without --watch as well)
I realized I have to access the dev page with app_dev.php:
localhost:8000/app_dev.php/
Try to execute:
php app/console assetic:dump --env=dev

Symfony 2.0 assetic dump produces wrong names in debug mode

I'm compressing my javascripts via assetic (block in twig for all scripts in one dir) which works fine in prod mode. Now i want to use the debug mode for my prod env, so i switch assetic to debug in the config, clear the cache and dump the assets with debug on.
This works for some javascripts but not all. Symfony adds a suffix number to them which is higher (by one) on the website (javascript tag) in opposite to the real file. Sometimes clearing the cache and dumping again solved the problem, but noot this time.
For example:
It dumps: /web/js/main_part_3_jquery-ui_6.js
But uses: /web/js/main_part_3_jquery-ui_1.js
So how can i solve this?
Edit:
The wron suffix doesn't appear on the first request to the site after cleaning the cache.
Since app/console assetic:dump is sensible for cached yml files - you should clear the cache for dumping assets each time you change configuration.
Best of all is to do it in this sequence:
rm -rf app/cache/*
app/console assets:install web
app/console assetic:dump
Of course, with debug keys, needed environments and so on
I had a similar problem of multiple generated assets and symfony not including the good one on display.
It was because of busting cache enabled in my case, and it was apparently misconfigured. (apparently it's usefull when you need a new version of your files, for example when you update your .js in dev but don't want to break prod )
So disabling it in config.yml fixed it.
assetic:
workers:
cache_busting:
enabled: false

Resources