Symfony2 disable cache? - php

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

Related

I have to replicate all config file for each environment

I try to add some config in config/* directory.
I'll use a very simple example, to illustrate the problem, but keep in mind that this problem occur for every .yaml config files for any services. I am not able to make inheritance between config files.
So in services.yaml I have this code :
#config/services.yaml
parameters:
app.debug: 0
smtp.user: ''
app.locale: '%kernel.default_locale%'
app.product.number_per_page: 20
container.dumper.inline_factories: true
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$locale: '%kernel.default_locale%'
$product_per_page: '%app.product.number_per_page%'
And then i try to access $product_per_page into my controller :
#src/Controller/myController.php
public function listProduct(int $product_per_page){
dump($product_per_page);
die();
}
When I execute this code, it give me the following errror :
Could not resolve argument $product_per_page of
"App\Controller\ProductController::listproduct()", maybe you forgot to
register the controller as a service or missed tagging it with the
"controller.service_arguments"?
So I have to copy this config files (config/services.yaml), and then paste it into config/services_dev.yaml, services_prod.yaml, services_test.yaml
And then it is working, but this is not suitable at all...
Because I have to do that for every config files... So i copy and paste in every environnement each time I update ANY configuration.
Of course, I have try to cache:clear and composer update, but nothing do.
I was expecting Symfony read the file as explain here but it is not working for me.
I have try to create another Symfony project, and now it is working in this one. But I want to understand what is wrong ? Is there a thing I have done which have result into a bug that cannot being correct even ater updating Symfony, even after having update composer, even after having clear the cache.
I can't believe there's nothing to do but to create a new Symfony project in which you copy the files from the old one.

Twig does not update changes in symfony

I am working with Symfony 2.8 in developer mode and Windows 10. When I update template Twig, always I have to clear cache to update render. I looked permissions for folders cache and logs and I have the necessary permissions.
Any help?
Go to 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.
Or you can disable the twig cache on web/app_dev.php only for avoid any problem when you're going to push your code in prod environnement.
You should try to execute the command below in terminal, in your symfony project directory.
php bin/console cache:clear
Symfony documentation about that:
https://symfony.com/doc/current/deployment.html

Symfony2 assetic:dump replaced urls in javascript files. How to avoid this?

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...

Different security.yml files for different environments

I could not manage to find a way to include different security.yml files that would be included depending on Symfony2`s environment. For example I wanted to have an in-memory user provider for my acceptance tests, cause I don't really need to test my entities and stuff here, I only want to make an acceptance test for my views.
But, as it turned out, it's not an easy thing to do. I removed security.yml from includes in my config.yml, renamed it to security_prod.yml and created a security_test.yml which has the in_memory user provider. Then I've included security_prod.yml and security_test.yml in my production and testing configs respectively.
Yet it does not seem to work at all:
$ SYMFONY_ENV=test app/console cache:clear
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
You are not allowed to define new elements for path "security.providers". Please define all elements for this path in one config file.
$ SYMFONY_ENV=prod app/console cache:clear
[Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException]
Configuration path "security.access_control" cannot be overwritten. You have to define all options for this path, and any of its sub-paths in one
configuration section.
It appeared to me like the security.yml filename was hardcoded (which would be way too weird for Symfony), and it wasn't.
So the question is: how do I get multiple security.ymls with Symfony? And what could be causing this behaviour?
Instruction for those, who is looking for it (and do not red comments):
Create different config files for different environments: config_test.yml, config_dev.yml, config_prod.yml
Create different security files: security_test.yml, security_dev.yml, security_prod.yml
Import security_test.yml in config_test.yml and so on for other environments. Example for config_test.yml:
imports:
- { resource: security_test.yml }
Make sure you included security_*.yml only once (basically author did this mistake)

How to make Symfony2 to load CSS, JS files directly and not via PHP?

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.

Categories