Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - to see changes in twig have to clear cache every-time

Tags:

php

symfony

I only have this issue now. Every time I make a change in my twig I have to cache:clear. as well as no errors are being shown if something is wrong in the code? What do I do?!

like image 365
dsuma Avatar asked Jul 08 '13 10:07

dsuma


2 Answers

I had faced this problem multiple times. If your website was accessing by so many user and you clear the cache. i am sure your website was down for couple for minutes until the new cache generated.

So clear cache on production server should not be a regular activity. There are couple of solution or tricks to overcome from this problem:

  1. find the time when your website has low traffic. May be sometime in the night and then clear the cache.
  2. When you want to clear a cache, setup a replica of a production server then plan to switch the public domain ip to the new replica for the timing so that user cant face the downtime and once you cleared the cache on a actually production server. switch the public domain ip back to the production server.
  3. if you do some changes in the templates i.e.twig and wants to made changes live on the production.Then try to find the templates in the app/cache/prod/twig directory and grep the templates name and you will get the files. Than move the files or delete the files and yours changes will live on the production server.

how to clear the cache

php app/console cache:clear 
chmod -R 777 app/cache
chmod -R 777 app/logs 

Alternative

You have to do some changes in app.php file located in web folder.

change

   $kernel = new AppKernel('prod', false);    

to

  $kernel = new AppKernel('prod', true);

and clear the cache

like image 82
Aman Varshney Avatar answered Oct 31 '22 16:10

Aman Varshney


I just created a console command to selectively list or delete twig cache files manually, instead of running a time consuming clear:cache which clears everything. The syntax is:

kmlf:twig --clear --env=dev AcmeBundle::nglayout.html.twig AcmeBundle:Simple:simple3.html.twig

you can eliminate the --clear flag if you just want to list the cache file locations. It seems to work well in both prod and dev environments for Symfony 2.3:

use Symfony\Component\Console\Command\Command; 
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;

class TwigCacheCommand extends ContainerAwareCommand
{

    public function configure()
    {
        $this->setName('kmlf:twig')
        ->setDescription('selectively manage the twig cache')

        ->addArgument(
            'names',
            InputArgument::IS_ARRAY,
               'Example AcmeBundle:Section:view.html.twig',
                null
        )->addOption('clear','c', InputOption::VALUE_NONE, 'delete cache files' );
    }

    public function write($output, $text) {
        $output->writeln($text);
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {

        $environment = $this->getContainer()->get('twig');
        $names = $input->getArgument('names');


        $actionName = null;
        if ($input->getOption('clear')) {
            $actionName = 'deleting';
            $action =  function ($fileName) {
                unlink($fileName);
            };
        } else {
            $actionName="path:";
            $action = function ($filename) {

            };
        }

        foreach ($names as $name) {

            $fileName = $environment->getCacheFilename($name);

            if (file_exists($fileName)) {
                $action($fileName);
            } else {
                $fileName = 'not found.';
            }
            $this->write($output, $actionName.' '.$name."\ncacheFile: ".$fileName);
        }
        $this->write($output, 'Done');
    }
}
like image 23
krewmarco Avatar answered Oct 31 '22 15:10

krewmarco