I'm using next command to generate static javascript/css files for production server in Symfony2:
app/console assetic:dump
Also in config I have next:
assetic:
debug: "%kernel.debug%"
use_controller: false
filters:
cssrewrite: ~
But I found that Assetic in Symfony also replace urls in javascript files. It is fine, but for some js libraries it it problem. For example for jsTree jQuery plugin. Here is place in code that Symfony understand incorrectly and I have in result something like:
node.childNodes[1].childNodes[0].style.backgroundImage = 'url('../bundles/bundlename/js/plugins/jsTree/+obj.icon+')';
I research Assetic configs on Symfony pages and even removed rules for css "cssrewrite: ~" from configs :) But no effect for js code. Probably someone know how I can deprecate to Assetic replace urls for javascript via config.yml? Or is there other solutions for current situation?
As variant for me - just modify this place in jsTree library to do not have problem with Symfony. But it is not good solution as for me...
Related
I am learning symfony2 been through AcmeBundle and under Resources/config there are two files routing.yml and services.xml i know what they are, my question is when I create my very own Bundle using php app/console cache:clear
I get to choose Configuration format (yml, xml, php or annotation) but by choosing either I always get both routing and services files in the same format.
I would like to always have routing.yml format and services.xml format.
I tried deleting routing.xml and added routing.yml but i can never get this to work....Any help guys
If you change the routing file in your Bundle, you need to tell Symfony to use the new one.
The main routing file is app/routing.yml (or .xml), and routing files specific to Bundles are loaded from this file.
So, if you want to use YAML files for routing in your Bundles, your app/routing.yml should contain something like this:
my_bundle:
resource: "#MyBundle/Resources/config/routing.yml"
prefix: /
Probably, if you're creating the routing file as XML and then you're just renaming it, your problem is that the app/routing.yml is pointing to the wrong file.
That said, I don't really know whether is possible to create the routing file as YAML and the services file in XML and, in fact, I don't think that's a very good idea... you should probably choose the format you like best and stick to it, so everything is more consistent...
I can't believe that I'm not able to google this...
So, I have a symfony2 application, and I installed jQuery UI with Composer. That means i have project structure like this:
/app/
/src/
/vendor/components/jqueryui/
/web/
I have assetic set up to copy js/css files from the vendor folder to web. That works fine. Config:
assetic:
assets:
javascripts:
inputs:
- %kernel.root_dir%/../vendor/components/jquery/jquery.js
- %kernel.root_dir%/../vendor/components/jqueryui/ui/jquery-ui.js
- %kernel.root_dir%/../vendor/components/jqueryui/ui/i18n/jquery.ui.datepicker-cs.js
stylesheets:
inputs:
- %kernel.root_dir%/../vendor/components/jqueryui/themes/redmond/jquery-ui.css
filters: cssrewrite
After assetic:dump i have
/web/js
/web/css
cssrewrite rewrites images/image.png to ../images/image.png. I guess that's ok, assumes a /web/images directory with said images.
Now the question is: How do I copy the images from /vendor/components/jqueryui/themes/redmond/images to /web/images?
Or is there another best practice to do that? Excuse me if it's and obvious thing, I'm a .NET guy, just trying out PHP.
My answer probably isn't spot-on because I haven't tried this with vendor files but this is what worked for me:
app/console assets:install
It copies all of your public resources to your web directory.
This answer by user1814739 helped me understand this and may provide additional information for you.
Also consider this advice from the Symfony Blog:
Although developers usually execute the command without any option, most of the time it's better to execute it with the --symlink option. This makes a symbolic link of your assets instead of actually copying their files. This means that any change in the content of the web assets will have immediate effect in the application.
I'm having an issue with assets management in Symfony2. I keep getting the following error :
Cannot load resource ".".
I've been trying to fix it in my config and routing files for dev environment, but the only thing I really did was changing the use_controller to false in the config_dev.yml file.
I keep summoning my resources in my templates with
{% stylesheets '#MyBundle/Resources/public/css/style.css'
filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
and I keep getting the exception :
Cannot load resource ".".
Any tips on what could go wrong? (I've cleared the cache several times)
Thanks in advance
Edit :
I've tried removing :
_assetic:
resource: .
type: assetic
from my routing_dev.yml file, and the error disappears (with, of course, the resources in my page)
Reedit :
After a complete reinstallation, it seems to be working again. Probably some property I had changed unvoluntarily... Case closed.
In my case it was related to LiipImagineBundle, I had these lines at my Proyect/FrontendBundle/Resources/config/routing.yml file:
_imagine:
resource: .
type: imagine
Try to run the following commands:
php app/console assets:install web
php app/console assetic:dump
In my case, I had an entry in routing.yml for an package I removed from AppKernel.php. After I removed the routing entry, the error was gone.
I remember having a similar issue some times ago, try to add this line in /app/config/config_dev.yml:
assetic:
use_controller: true
Then clean your cache:
php app/console cache:clear
Then:
php app/console assets:install web
I had the same issue. In my routing.yml was the following:
api:
resource: "."
type: "api"
prefix: "/api"
After removing these lines, the issue was fixed.
Check your assetic configuration,
if your going to use
assetic:
use_controller: false
Then check your routing file, comment out the lines
_assetic:
resource: .
type: assetic
That routing entry is not needed when not using assetic's auto compiling.
OLD QUESTION, SEE BELOW FOR THE UPDATED VERSION
My development environment is not the fastest. I takes roughly 500ms per PHP request. It's starting to become a problem with Symfony2 resource files because each of the resource files are being requested via Symfony's internal controllers:
http://localhost/myproj/app_dev.php/js/bb8690a_part_4_myJavaScriptFile_2.js
As can be seen, the files are loaded via the Symfony framework and not directly. Since I'm starting to have over 20 files to load, multiplying that with the 500ms makes page loads very slow. I want to load the files directly, but I am not sure how to do that.
This is part of the config.yml:
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
# java: /usr/bin/java
filters:
cssrewrite: ~
I thought setting use_controller to false would do it, but nope.
Is there a way to handle the loading of those resoures directly?
UPDATE:
This is the URL it tries to use now:
http://localhost/myproj/_controller/js/bb8690a_part_4_myJavaScriptFile_2.js
I have set use_controller to false for both dev and general configs. How do I get rid of that _controller part of the URL?
Edit: If I clear the cache, run assetic:dump and have use_controller as false, then upon reload I get Cannot load resource ".". I can't get around that problem unless I temporarily enable use_controller for one page load. After that, I disable it and reload and now it requests from that invalid URL that contains _controller.
It also seems to work in prod, but not in dev. Strange.
Template code:
{% stylesheets filter="cssrewrite"
'bundles/outotecofil/css/reset.css'
'bundles/outotecofil/css/*'
output='css/dist/dist.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% javascripts
'#OutotecCommonBundle/Resources/public/js/jquery-1.6.2.min.js'
'#OutotecCommonBundle/Resources/public/js/jquery-ui-1.8.16.custom.min.js'
'#OutotecCommonBundle/Resources/public/js/chosen.jquery.min.js'
'#OutotecCommonBundle/Resources/public/js/widget/*'
'#OutotecOFILBundle/Resources/public/js/OFILDependencyManager.js'
'#OutotecOFILBundle/Resources/public/js/widget/*'
'#OutotecOFILBundle/Resources/public/js/plant-scope.js'
output='js/dist/dist.js'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
To be extremely clear: without app_dev.php (i.e. in prod mode), it works. Only in dev it does not and throws this "Cannot load resource "."" error unless I first enable use_controller for one request, after which I can disable it and reload though the URLs will then contain _controller/ in their paths.
Try to remove this part of code in routing_dev.yml when use_controller is false :
_assetic:
resource: .
type: assetic
Symfony documentation is always the first place where to start look: How to Use Assetic for Asset Management
In the prod environment, your JS and CSS files are represented by a single tag each. In other words, instead of seeing each JavaScript file you're including in your source, you'll likely just see something like this:
<script src="/app_dev.php/js/abcd123.js"></script>
Moreover, that file does not actually exist, nor is it dynamically rendered by Symfony (as the asset files are in the dev environment). This is on purpose - letting Symfony generate these files dynamically in a production environment is just too slow.
Instead, each time you use your app in the prod environment (and therefore, each time you deploy), you should run the following task:
php app/console assetic:dump --env=prod --no-debug
This will physically generate and write each file that you need (e.g. /js/abcd123.js). If you update any of your assets, you'll need to run this again to regenerate the file.
If you use assetic:dump, then you have to cache:clear -e dev
"...if you run cache:clear on your production cache, it warms up the cache with debug mode on. If you try to dump assets afterwards, weird things might happen."
i found it here: http://sftuts.com/using-assetic-in-symfony2-for-css-compression (4. paragraph)
From the documentation
Modify the dev config to avoid using the controller.
# app/config/config_dev.yml
assetic:
use_controller: false
Remove the route in routing_dev.yml to avoid side effect
# app/config/routing_dev.yml
_assetic:
resource: .
type: assetic
Automatically dump your css/less files every time you have a modification.
php app/console assetic:dump --watch
I have the same problem, working configuration is:
comment out from routing_dev.yml:
_assetic:
resource: .
type: assetic
set use_controller to false.
After doing this I'm able to use assetic:dump and see working page.
Is there a way to disable the caching function in Symfony2? I tried to find the setting in the config* and parameters.ini files and I searched a lot. Ok, I found a few solutions, but nothing for the latest version (Symfony2).
WHY? Because I want to test new templates and functions without clearing the app/cache* all the time.
I'm assuming you're using the Twig engine, (the default templating engine for Symfony2). To disable caching in twig, so that you do not have to keep clearing the cache like so:
rm -rf app/cache/*
Navigate to your app config file (by defualt will be located in ../app/config/config.yml from your root directory). Scroll to the twig configuration settings (under twig:) and change the cache value (which should be pointing to the cache directory) to false like so:
twig:
cache: false
If you do not see any cache configuration entry, simply add the line above.
It may also be helpful to checkout the configuring reference for the Twig bundle: http://symfony.com/doc/2.0/reference/configuration/twig.html
After editing your config_dev.yml file, go to your terminal and run:
app/console cache:clear
Okay, regarding your clarification the solution simply is to use the dev-environment through the front-controller web/app_dev.php. Then sf2 keeps track of your adjustments and you don't have to clear the cache.
This original solution works for me http://symfony.com/doc/current/cookbook/debugging.html
In addition to the accepted answer, I propose to edit your config_dev.yml in a way so it still debugs your twig template. To do so, add this code to your config_dev.yml file:
twig:
cache: false
debug: true
services:
twig.extension.debug:
class: Twig_Extension_Debug
tags:
- { name: 'twig.extension' }
After editing your config_dev.yml file, go to your terminal and run:
app/console cache:clear
By doing so, you will reload your config_dev.yml settings - make your project run with the new configuration.
Hope this helps.
Edit 'config_dev.yml' and 'config.yml' and then put in both
twig:
cache: false