Stop caching in Symfony - symfony

In my/app/cache, every time I edit a twig file I have to delete the 'prod' folder.
Is there a way to turn caching off?
Any help would be appreciated.

Although like stated use dev and if you really can't. Twig has a option for it. I have highlighted the line for you.
Quoted from http://symfony.com/doc/3.4/reference/configuration/twig.html#cache
cache
type: string default: '%kernel.cache_dir%/twig'
Before using the Twig templates to render some contents, they are compiled into regular PHP code. Compilation is a costly process, so the result is cached in the directory defined by this configuration option.
Set this option to null to disable Twig template compilation. However, this is not recommended; not even in the dev environment, because the auto_reload option ensures that cached templates which have changed get compiled again.

You can also leave in prod env, but set debug:true and this will force the templates to always recompile.

Related

Prestashop: SMARTY - force compilation / recompile when files are modified

I'm working on a Prestashop site and recently noticed about the SMARTY features (Menu: Advanced > Performance).
I realized that in order to show changes I made in the css, I must select one of the last two options (first one was selected by default).
Screenshot (I'm sorry the site is in Spanish)
1)Never recompile template files
2)Recompile templates when files are modified
3)Force compilation
My question is: when it comes to an online shop, wich option should I select until I finish editing the code? What's the difference between both?
It may take me a couple weeks to finish the job and I don't want to mess anything up.
Thank you guys.
When you're starting to dev onto the shop, whether it's front or back, you may have to choose the option to recompile when files are modified. I'm always choosing this option because it allows me to develop or debug some files and the server keeps serving cache files to the visitors.
Also you may have to edit the file defines.inc.php file in the config folder in order to define the _PS_MODE_DEV to true, for example like this :
/* Debug only */
if (!defined('_PS_MODE_DEV_'))
if (in_array($_SERVER['REMOTE_ADDR'], array('217.128.240.59')))
define('_PS_MODE_DEV_', true);
else
define('_PS_MODE_DEV_', false);
Doing this so you'll be able to get some logs when you're updating something. Placing your IP into the array keeps everyone safe from seeing the logs (notices for example).
In PS 1.6.
- Configure the SMARTY to "Recompile templates if the files have been updated" then deactivate the option "Smart cache for CSS"
- make the changes in your CSS files,
- delete the folders mentioned sadlyblue in comment.
- and activate again the "Smart cache for CSS" SAVE to recompile theme.

What is the purpose of cached Assetic config?

I'm doing some research into Assetic and asset-management generally in Symfony. One thing I've noticed in the cache folder for my project (which uses Assetic) in the dev environment is the folders assetic\config, with that config folder having subfolders 0-9 and a-f.
Inside each of those subfolders are a number of PHP files with hashes for filenames and contents like this (the comment is part of the file):
// MyBundle:FolderName/twigfilename.html.twig
return array (
);
General questions: What is the point of these files? What do they do? Will they ever have more interesting content than this? (I've never found any that do.)
More specific question: Unless use_controller is set to false (ref), assets are loaded dynamically every time, so - as I understand it - you shouldn't need to clear the cache to force an update of your assets. My general rule-of-thumb is "don't bother clearing the cache for things to do with assets, just do that for Twig or PHP-file things". Given that Assetic obviously puts something in the cache, is this still a good rule-of-thumb?

Symfony translations not working

I have done the following checklist:
created translation file respecting format domain.lang.loader
cleared cache
checked that language catalogue is created in cache folder
Though in my twig template file,
{{ 'message'|trans }}
never translates.
Where can I look next in order to make translations work?
Is there any chance that Doctrine Translatable Extension that I am using generates some kind of conflicts?
In Symfony 3.0 I had to clear the cache:
php bin/console cache:clear
I see you already did that, maybe it helps other like me.
Have you enabled the Translator service in your config file?
framework:
translator: { fallbacks: en }
The language catalogue is created in your cache folder irrespective of whether your translator is enabled or not.
Did you try translating in your controller?
$trans = $this->get('translator')->trans('message');
Try to specify domain. If you not specify domain by default it a messages.
{{ 'message'|trans({}, 'some_domain') }}
Then translations can be found in
the kernel root directory/Resources/translations directory;
the kernel root directory/Resources/bundle name/translations
directory;
the Resources/translations/ directory of the bundle.
For example some_domain.fr.yml. Last step is to configure your locale. You can get current locale from request with $request->getLocale()
P.S. try to rm -r app/cache to make sure that the cache is deleted
I could use one of the translations, but not the other and didn't know why. If you have troubles with translations also, read this.
First, standard checklist:
Make sure you enabled and configured translator.
Make sure translation is in proper place and follows proper naming convenction ( domain(messages by default).lang_code.file_format ).
Clear cache using php app/console cache:clear command.
Try to manually call $this->getRequest()->setLocale('en'); in Controller, also you may try to use $this->get('translator')->trans('Some message'); directly in your Controller.
If it still doesn't work, make sure BOM isn't in your translated file. That was my case.
Watch out for BOM in the translated file. The translator who translates the yml file used UTF8 which is OK, but editor he used leaved BOM at the beginning of the file. This is dangerous probably because of PHP's UTF8 BOM bug as it adds few invisible characters to first section of your file.
Btw, debugging your translations may be very helpful, too.
According to the Symfony Translations Documentation page, if you are not using a Service Container for your translation purpose, these are simple steps to go:
Enable and configure Symfony's translation service.
YAML
framework:
translator: { fallbacks: [en] }
PHP
$container->loadFromExtension('framework', array(
'translator' => array('fallbacks' => array('en')),
));
Abstract strings (i.e. "messages") by wrapping them in calls to the Translator ("Basic Translation").
public function indexAction()
{
$translated = $this->get('translator')->trans('Symfony is great');
return new Response($translated);
}
Create translation resources/files for each supported locale that translate each message in the application.
Symfony looks for message files (i.e. translations) in the following default locations:
the app/Resources/translations directory;
the app/Resources/<bundle name>/translations directory;
the Resources/translations/ directory inside of any bundle.
Translation File Name
The filename of the translation files is also important: each message file must be named according to the following path: domain.locale.loader (e.g. filename: navigation.en.xlf):
domain: An optional way to organize messages into groups (e.g. admin, navigation or the default messages) - see Using Message Domains;
locale: The locale that the translations are for (e.g. en_GB, en, etc);
loader: How Symfony should load and parse the file (e.g. xlf, php, yml, etc).
The loader can be the name of any registered loader. By default, Symfony provides many loaders, including:
xlf: XLIFF file;
php: PHP file;
yml: YAML file.
The choice of which loader to use is entirely up to you and is a matter of taste. The recommended option is to use xlf for translations.
Determine, set and manage the user's locale for the request and optionally on the user's entire session.
Clear the cache:
php bin/console c:c
The Translation Process
To actually translate the message, Symfony uses a simple process:
The locale of the current user, which is stored on the request is determined;
A catalog (e.g. big collection) of translated messages is loaded from translation resources defined for the locale (e.g. fr_FR). Messages from the fallback locale are also loaded and added to the catalog if they don't already exist. The end result is a large "dictionary" of translations.
If the message is located in the catalog, the translation is returned. If not, the translator returns the original message.
This helped me to get it worked, since clearing the cache also didn't help.
Symfony 4.6.2:
Try this command to update translation files:
php bin/console translation:update --dump-messages --force de
(Source: https://symfony.com/doc/current/translation.html#configuration)
I can already answer your 2 questions:
1: you can look at
https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php#L97
https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Translation/Translator.php#L174
2: If you're talking about gedmo doctrine extensions, or Knplabs DoctrineBehaviors, no, there is no way it conflicts with symfonys's translator. These are 2 independant pieces.
Just faced the same issue and fixed it by $this->get('translator')->setLocale('fr'); in the controller action. I fixed it by adding {_locale} in the route path.

Symfony cannot find template on prod server

Should be simple enough. Everything works in my local environment, but not on my prod server (neither prod nor dev environment). I get an error that the FOS template cannot be found:
Unable to find template "FOSUserBundle::layout.html.twig".
My code is simple:
{% extends "FOSUserBundle::layout.html.twig" %}
I'm going from my local Windows machine to a Linux box. I've read case sensitivity might be the culprit, but it doesn't seem like it.
The FOS bundle is in the normal place: vendor/friendsofsymfony/user-bundle/FOS/UserBundle.
I'm having trouble diagnosing - any ideas? Did things get screwed up when I FTPd? Permissions issues? What are the common gotchas? I'm desparate!
Update: A clue. I ran assetic:dump and got
[RuntimeException]
".../app/Resources/FOSUserBundle/views/layout.html.twig" resource is hidden by a
resource from the "*******Bundle" derived bundle. Create a
".../app/Resources/*******Bundle/views/layout.html.twig" file to override the bundle
resource.
This question seems relevant.
Another update: I was using getParent() in my own bundle to override the templates. I switched to the first method in the docs but now the templates in app/resources simply aren't having an effect. It's going straight to the default form template with a white background.
Any ideas?
It was a upper/lower case/namespacing thing. I thought my local files were in sync with remotes but they were not.
Just check your whole directory & all namespaces in the FOS bundle!
I also encountered the problem and "solved" it by renaming my layout.html.twig inside my bundle's Resources/views folder to bundle-layout.html.twig and adapting my templates accordingly.
I resolved the resource is hidden by a resource error by moving the template in the good bundle extension.
Specifically from app/Resources/FOSUSerBundle/views/ChangePassword
to src/Application/Sonata/UserBundle/Resources/views/ChangePassword
I confounded FOSUser & SonataUser.

Slashes being added with Symfony2 form builder - where do I use stripslashes()?

I'm building a small website with Symfony2 (i.e. not Symfony 1.x). I used the default CRUD generator from an entity created using the CLI generator. However, when it's saved to the database, it's being saved with escaping slashes.
Where is the right place to stop that happening? In the entity, the repostiory, the controller or the form? Is there some magical function for doing it?
Turned out to be nothing to do with Symfony. I'm using MAMP, and magic_quotes_gpc was turned on. For future reference, go to your php.ini file and set
magic_quotes_gpc = Off
To check whether magic quotes are turned on, run just do
if(get_magic_quotes_gpc()) { die('magic quotes turned on'); }

Resources