Tutorial :Different version of the cache in Symfony



Question:

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?


Solution:1

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;  }  


Solution:2

I'm sure you solved this by now, and the app is already upgraded to the latest version. But I solved a similar problem generically by including a filter that sets a user-specific parameter in every URL preventing the data leak. This destroys reporting in GA, which is my current problem.

// Filter class in apps/frontend/lib/accessFilter.class.php

<?php  class accessFilter extends sfFilter  {     public function execute($filterChain)     {        $context = $this->getContext();        $context->getRouting()->setDefaultParameter('sw_user_id', $user_id);              $filterChain->execute();     }  }  

// Filter definition in apps/frontend/config/filters.yml

# insert your own filters here  accessFilter:    class: accessFilter  

// Use within routes in apps/frontend/config/routing.yml

dashboard:    url:   /dashboard/:sw_user_id/home    param: { module: dashboard, action: index }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »