I want to invalidate the HTTP cache in symfony2. I use the following method:
protected function invalidateCache($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $status == 200;
}
That works, no problem. But when I use a ESI include I with the controller() function (not path()) like:
{{ render_esi(controller('AcmeDemoBundle:Default:index')) }}
How do I get the url generated by the controller function? Or how can I invalidate the cached response of that esi request?
So here is how you do it: You don't.
The reason why I wanted to use the controller() function instead for path() is because Symfony will protect the URL from controller() from unauthorised requests. What you should do is to use path() and prefix the URLs with "esi/" and then protect that URL in your security.yml.
//app/config/security.yml
security:
# // ---
access_control:
- { path: ^/esi/.*, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
If you want to clear the cache you just use the url as you normally would.
Thank you @jongotlin on Twitter for helping me with this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With