Clear Symfony2 Http Cache - symfony

Is there an easy way for fast removing http cache in symfony2? We've over 30.000 files in cache directory and removing them tooks very long. Is there an better way for doing this? Btw...linking the cache to /dev/null when removing...

The easiest way to clear cash would be to use console command:
app/console cache:clear
If this is production – you need to add environment (using paramentr --env=prod. )
As default all console commands run in the dev environment.
So, for example, this command looks like app/console cache:clear -e=prod.

Would always recommend using the Symfony console:
php app/console cache:clear --env=prod
This should be fastest, as it moves/renames your current cache folder and creates a new one before deleting your old cache, so there should zero downtime.

If you look in Symfony's cache directory /var/cache/ you'll find an http_cache directory.
So you could use PHP's exec() to remove the directory.
$root = $this->get('kernel')->getRootDir();
$path = $root . '/../var/cache/prod/http_cache';
exec('rm -rf ' . $path);

Related

How to properly check if a Symfony application is installed?

In our Dockerfile's entrypoint.sh I need to check (actually the database) to see if there's an application currently installed and if not run a php app/console command.
This is my current approach:
CHECK=$(php app/console doctrine:query:sql "SELECT * FROM user" -q)
if [ $? -ne 0 ]; then
php app/console vendor:install
fi
Which doesn't feel right. What other ways is out there to properly check this?
NOTE That the database is created by a 3rd-party MySql broker service. So I can't check for database existence, 'cause always is true.
Based on Composer hooks, you can check for these files and directories.
Required:
app/boostrap.php.cache or var/boostrap.php.cache.
app/config/parameters.yml.
vendor/symfony.
Check if app/cache or var/cache has been warmed up.
Optional:
Check if executing app/SymfonyRequirements.php or var/SymfonyRequirements.php would return a success - this may fail however for some strict requirements.
web/bundles if there are any assets to install.

Symfony 2 cache stuck after config change (FileLoaderException)

Very strange behavior in Sf2 here. I've try to change my config namespaces while i was refactoring code:
adadgio_rocket:
foo: 'bar'
to something simple
adadgio:
rocket: 'bar'
I have change my bundle DI Configuration nodes to reflect that change :
$rootNode = $treeBuilder->root('adadgio');
$rootNode->children()
->scalarNode('rocket')->end()
->end();
Now i have a FileLoaderLoadException still saying able to load the configuration for adadgio but still finds the adadgio_rocket namespace (error results).
I tried to clear the cache thinking this was the issue, but of course, the symphony cache command issues the same error. And its still finds other config namespaces that were removes (and their declaration in the AppKernel as well).
Basically, everything is stuck now.
Yes, that sort of stuff can happen. Simply delete the app/cache/dev directory and run app/console cache:clear again.
If it still doesn’t work, make 100% sure that there are no remainders in the config or referenced in other bundles. If unsure, check:
grep -ir adadgio app/config/ src/ vendor/
It can be a issue of permission also. Give 777 permission to cache and use following to clear cache without warming up the cache:
app/console cache:clear --env=<your environment> --no-warmup

Symfony2 - confused cache clear trying to write to pro_

My Symfony installation seems to have gotten confused somewhere (probably during a failed cache clear). It seems to think that the production cache folder should be called pro_ instead of prod.
First, when I tried to run $ php console cache:clear --env=prod I got the error message:
[Symfony\Component\Filesystem\Exception\IOException]
Cannot rename "...cache/pro_" to "...cache/pro_".
I'm running on a windows dev machine (so there are no file system permissions issues).
So, I tried deleting all of the cache/* files/folders manually and trying again. This time the cache:clear went through and produced a single prod folder. That folder has subfolders for annotations, assetic, doctrine, sessions and twig.
But when I open the prod front controller (e.g. http://devsite/ in a browser) it fails with the message:
Fatal error: require() [function.require]:
Failed opening required '...cache/pro_/doctrine/orm/Proxies\__CG__AcmeDemoBundleEntityFoo.php'
(include_path='blah, blah, blah')
in ...\vendor\doctrine\common\lib\Doctrine\Common\Proxy\AbstractProxyFactory.php on line 165
From the looks of this it's trying to access cache files in pro_ instead of prod (again).
Looking in the cache folder I can see that __CG__AcmeDemoBundleEntityFoo.php exists under cache\prod but there is now a pro_ folder with annotations and sessions sub-folders.
What's going on and how do I make Symfony forget about pro_ so it can go back to using prod for everything?
I thought everything to do with the cache was stored under the cache folder... is there something else (somewhere) that's storing some reference to pro_? Or am I looking in the wrong place for the solution to this problem?
Edit: Done some more searching and appProdProjectContainer.php contains 16 references to .../cache/pro_. If I manually search and replace these to .../cache/prod the site works. But the next time I run console cache:clear it resets them back to pro_. Where could this errant behaviour be coming from?
Edit2: OK, I got to the bottom of this and have submitted a PR to the Symfony core to try and fix it. The problem was caused by our cache path containing '\' characters which were then escaped in the cache files and failing to match a search/replace command which was meant to clean them up.
app/console cache:clear --env=prod --no-warmup
composer dump-autoload -o
app/console cache:warmup --env=prod
This was a bug in Symfony's handling of cache paths with back-slashes in them.
The short term workaround is to replace them with forward slashes.
Longer term there is a fix posted as https://github.com/symfony/symfony/pull/9184 which will be brought into the core code soon.

Assetic - Route "_assetic_001d5b7_0" does not exist

This question seems to have been asked multiple times but none of the solutions work for me.
I'm in my prod environment, here is what I've done:
cleared cache before/after doing anything
attempted commenting out the _assetic stuff in config_dev and ensure it isn't anywhere else (not that this should matter in prod env)
set use_controller to both true and false (obviously works with true but doesn't use the compiled files)
Is there anything else I'm missing? The files are generating completely fine from
php app/console assetic:dump --env=prod --no-debug
the file name matches that of in the error minus the route stuff.
I had this problem just one minute ago. Clean up the cache worked for me:
app/console cache:clear --env=prod
Hope this helps.
If clearing the cache or dumping the assets doesn't work. Try noisebleed's comment:
// app/config_dev.yml
assetic:
use_controller: true
bundles: ['FooBarBundle']
Maybe you have removed the assetic routing from the app/routing_dev.yml
_assetic:
resource: .
type: assetic
Faced with the same issue but reason was I name template as "something.twig", not "something.html.twig".
Its seems assetic not scan templates without .html in extension.
As result - template work, but assetic not perform dumping/adding routes for assets from it. Adding .html solve the problem.
Updating the config.yml with a dumb character (newline, white space) remove that error. It seems that by doing that, we force the cache do be re-generated. (Symfony 3.0.0)
maybe is too late but... what worked for me:
php composer.phar install
php app/console cache:clear
php app/console cache:warmup
Like #Marcus said, if you tried:
php bin/console cache:clear
and it didn't help, please clear the your_project_root/var/cache folder manually (remove all folders and files). If you use unix/linux systems, and receive a system message like "Error removing file: Permission denied", you need to change accesses to dir first, for example you can use console command
sudo chmod -R 0777 /your_site_directory/var/cache/
and after this you can clear cache dir.
I encountered this issue in Symfony 3.3 after trying to override my Twig templates directory. Not sure how to fix the issue, but reverting to default templates directory config resolved the issue for now.
# config.yml
twig:
paths: ["%kernel.project_dir%/templates"] # Remove this line to use default dir
After trying all the suggested solutions here, to me it was simply the issue with the name of the template. I had .twig but not .html.twig extension, that's all.

Symfony2, How to load fixtures with --fixtures option?

I need an example for command below in Symfony2 :
php app/console doctrine:mongodb:fixtures:load --fixtures=/path/to/fixture
I don't know how to set the --fixtures=/path/to/fixture. I can load all together but I cannot do it for only one new fixture !
What format should /path/to/fixture be ?
Also I tried this --fixtures=src/PMI/UserBundle/DataFixtures/ORM/FeatureFixtures but I get this every time :
[InvalidArgumentException]
Could not find any fixtures to load in:
- src/PMI/UserBundle/DataFixtures/ORM/FeatureFixtures
I know this is old but other people have asked and these answers may not cover some of the other questions.
You only need to specify the folder, not the file. In the above question, its not clear if FeatureFixtures is the php file or actually a folder. It may be failing if that is only a php file and not a folder. This is the correct way to load the fixtures assuming that FeatureFixtures is a php file with the fixtures:
doctrine:fixtures:load --fixtures=src/PMI/UserBundle/DataFixtures/ORM --append
You can define a fixture with the real path.
php app/console doctrine:fixtures:load --fixtures=src/MyBundle/DataFixtures/ORM/LoadMyFixturesData.php
It's not require but if you want to manually specify the directory where the fixtures classes should be loaded you can use this :
for example if I execute a command in directory symfony
--fixtures=/src/yourBundle/fixture
Starting with DoctrineFixturesBundle version 3.1 you need to use doctrine:fixtures:load --group=FeatureFixtures.
For symfony +3 you can use this command:
php bin/console doctrine:fixtures:load --group=YourFixtures --append.
Or short version:
php bin/console d:f:l --group=YourFixtures --append.
where option:
--group Target fixture. If you do not use the option, then load all your fixtures;
--append Without this option the load fixture command will remove all data from your database. Use this option to add new data without removing old data.

Resources