setono/twig-cache-purger-bundle
A Symfony bundle that keeps your Twig cache fresh by purging compiled templates automatically—on deploy or when templates change—so you never serve stale markup or manually clear cache directories again. Drop-in integration through Symfony's cache purger contracts.
This is a Symfony bundle, so the first thing a Laravel developer needs to do is translate the concept rather than expect drop-in reuse. The package solves one narrow problem: Twig compiles every template into a hashed PHP class in its cache directory, and normally the only way to invalidate that cache is to wipe the whole directory. This bundle exposes a service (and typically a console command) that lets you purge the compiled cache for one named template without touching the rest.
Where to look first:
src/ for the TwigCachePurger (or similarly named) service — that is the actual API surface you will call.DependencyInjection/Configuration.php for the config tree (cache directory / pool wiring).setono:twig-cache-purger:purge <template>.Minimal Symfony steps:
composer require setono/twig-cache-purger-bundleSetono\TwigCachePurgerBundle\TwigCachePurgerBundle in config/bundles.php.twig.cache pointing at a real directory); the purger cannot do anything useful against cache: false.Laravel reality check: Laravel uses Blade, which compiles to storage/framework/views/*.php, and Laravel only ships php artisan view:clear (all-or-nothing). So the day-to-day value here is conceptual: if you use Twig in Laravel (e.g. via rcrowe/twigbridge), you can adopt the bundle's pattern directly by purging the matching compiled file. If you are on Blade, treat this as a blueprint for a "purge one view" helper you would build yourself.
First use case to aim for: after an admin edits a single CMS template, purge just that template's compiled artifact instead of flushing the entire view cache (which, on a busy app, causes a cold-start recompile storm).
1. Inject the purger and call it from the save path.
The cleanest pattern is to autowire the purge service into the controller/service that persists a template edit and call purge($templateName) right after the write. Keep the template name as an explicit, resolvable string (e.g. pages/about.html.twig), not a Symfony logical path (@App/about.html.twig) unless the purger resolves namespaces.
2. Event-driven purge.
In Symfony this is an event subscriber on your entity's postUpdate/postPersist; mirror it in Laravel as an Eloquent observer or model event that calls a ViewCachePurger::purge($view) service. This decouples "content changed" from "cache invalidated" and is the pattern most CMS integrations end up with.
3. Deploy-pipeline purge.
Wire the bundle's console command into your release script for the handful of templates that change independently of code (email templates, marketing pages, translated stubs). This is safer than cache:clear because it does not evict unrelated Twig/DI caches and avoids a fleet-wide recompile.
4. HTTP/admin hook.
Expose a small authenticated endpoint or artisan command (php artisan view:purge {name}) that wraps the same service, so ops or a headless CMS can invalidate a single template without shell access.
5. Multi-tenant isolation.
If tenants have their own template directories, prefix template names per tenant (tenant_42/home.twig) and purge by exact name. Do not attempt directory-level purging through this API — it is a single-template tool.
6. Laravel port sketch.
Build a ViewPurger that computes the compiled Blade filename (sha1($path).php via the compiler's getCompiledPath) and deletes it. For Twig-in-Laravel, use Twig's Environment::getCache() + the template's class name to locate the file. Wrap it behind a service and an artisan command so call sites match the Symfony bundle's ergonomics.
@Bundle/...) or dynamically chosen templates may not map cleanly. Log what you pass and verify against the actual filename in the cache directory.unlink() files. Confirm which cache adapter your twig.cache uses before trusting the bundle.auto_reload/debug masks the problem. In dev, Twig recompiles changed templates automatically, so a broken purge looks fine locally and fails silently in production. Test with APP_DEBUG=0 and a warm cache.view:clear on multi-server Laravel setups.twig:cache:warmup (or Blade precompile) for high-traffic templates.view:cache artifacts. Keep it scoped to the "one template changed" workflow.How can I help you explore Laravel packages today?