Symfony2: disable Twig keg

I am trying to disable the cache key in prod mode or make it recompile my views.

I use KnapLaps SnappyBundle to create some PDF files (the same problem occurs with DomPDF), and I have dynamic content for rendering.

In dev mode, I can change some text or even some css properties, the changes take effect immediately.

But in prod mode, I need to cache: clear or rm -rf app / cache / prod / twig / * to see the changes.

I tried the following options in my config.yml for the Twig section (not at the same time)

cache: "/dev/null" cache: false auto-reload: ~ 

I am also trying to create heading material when creating and replacing my pdf:

 $html = $this->renderView("xxxxPdfBundle:Pdf:test.html.twig", array("foo" => $bar)); return new Response( $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 200, array( 'Cache-Control' => 'no-cache, must-revalidate, post-check=0, pre-check=0', 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename='.$file ) ); 

I can’t figure out how to make the branch recompile or not use the application / cache, because obviously the contents of the pdf will be dynamic during production.

Update from comments:

I realized that even dynamic template variables were not updated, so the same PDF file was generated again and again in the production process, but not in development.

After clearing all the caches, this problem was fixed again: now PDF files with dynamic content are created, as was developed.

However, the question remains: what if, when my site is in production, I decided to change the CSS style in pdf format? CSS is not a template variable, and I cannot get people to clear their cache: /

+7
php caching pdf symfony twig
source share
2 answers

The client-side caching question has several answers.

First, HTTP uses some headers that describe to the client how to perform caching. The worst thing is to declare that the resulting resource should be considered cached the next time by X without checking for updates again. A less intrusive version is to add a caption with the signature of the delivered version or the latest timestamp, and the client must check every time, regardless of whether the resource is updated before it is used.

The first type of caching can only be updated by deleting the client’s cache in the browser. The second one could probably be circumvented by forcing the page to load again (Ctrl-F5 or so), but it really is just as hidden as the menu to clear the cache.

To play in a safe place, the usual approach was to add a tag, revision number, incrementing counter or whatever is available to the query string of the URL used for this resource.

The first URL is from starting deployment 1234, the second is from 1235 - this number changes the URL enough to initiate a new request instead of getting the old version from the cache.

I do not know if there is anything in your system to act like that. You can also always add a constantly changing value, such as the current timestamp, to avoid caching at all if you cannot disable the HTTP caching headers.

+2
source share

The correct way to disable Twig caching is to set the cache environment parameter to false instead of the cache directory:

 # config_dev.yml # ... twig: cache: false 

Literature:

Twig Environment Settings

TwigBundle Configuration

+13
source share

All Articles