Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invalidate cache for a controller url?

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?

like image 595
Tobias Nyholm Avatar asked Nov 01 '22 03:11

Tobias Nyholm


1 Answers

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.

like image 97
Tobias Nyholm Avatar answered Nov 08 '22 06:11

Tobias Nyholm