views:

48

answers:

1

I'm new in Symfony and I have a problem with logical code organisation.

The problem is connected with cache and different version of webpage for guests, logged in users and owner.

For example. I have 'user' module, which has 'show' action, and the URL is /user/show/:id and URL is the same for every visitor. But the content of the page depends on visitor and is selected with 'if' conditions so... If I clear the cache and the first visitor is guest, then others (including owner and logged in users) will see the guest's cached page.

Some kind of solution can be separating each view (owner, guest, logged in user) to partial, but it's against the DRY rule.

How to do this?

+2  A: 

You can use the sf_cache_key parameter. See here how. I think you could use the user_id for logged in user, prepended with an arbitrary string for the owner, and for the guests, the string "guest" would do.

A bit of pseudo-code to help you further:

$sf_cache_key = '';
if ($visitor->isLogged())
{
    if ($visitor->getId() == $userId )
    {
       $sf_cache_key = 'owner' . $userId;
    }
    else
    {
        $sf_cache_key = 'logged_in' . $userId;
    }
}
else
{
    $sf_cache_key = 'guest' . $userId;
}
greg0ire
I have already readed documentation carefully.Case study :)There is profile page (user/show/1), with menu etc and if I (id=2) open that page(user/show/1) it will be cached. As a guest in his page I see buttons like "add to friends" or "send message". When user with id=1 opens his home page he will see cached page with buttons:/A simple solution would be diffrent cache for owner, guests an friends but is it possible?Ofcourse I'm using mentioned sf_cache_key, but I don't know how to use it in this case. I thought it is only useful in removing parts of the cache..
Marcin
I have edited my post to show you how to use sf_cache_key in this case. You just have to specify as many different cache_key as different cache versions you want.
greg0ire
I hadn't noticed you have edited post. solution was so simple.. Many thanks to greg0ire:)
Marcin
Everything is always very simple in Symfony ;-)
greg0ire