views:

32

answers:

2

Hi,

I have a custom routing class that checks versioning of an object to allow for creation of draft versions of pages that wont appear on the live site. When an admin clicks to preview a draft version of a page my PublishingHelper class on the front-end (called from the routing class) checks the user's permissions to see if they are allowed to view the draft version of this page.

I am using this code:

$context = sfContext::getInstance();
$user = $context->getUser();

But $user is NULL.

Does anyone have any ideas? All my searches seem to say this is the right way of getting the user object.

Thanks,

Tom

A: 

Thanks for the comments Till/Jon, Ive managed to fix this now. The factories fix didnt work because while the user class is instantiated none of the filters have run therefore I was left with a useless user object.

I solved my problems simply by taking pretty much all the code in the matchesUrl() function of my custom routing class and putting in a new function doRouting() in the same class. matchesUrl() now looks like this:

public function matchesUrl($url, $context = array())
{
  if (false === $parameters = parent::matchesUrl($url, $context))
  {
    return false;
  }

  $parameters['module'] = 'content';
  $parameters['action'] = 'route';

  $this->url            = $url;
  $this->context        = $context;

  return $parameters;
}

and the routing is deferred to after the factories and filters by using my "content" module/controller:

class contentActions extends sfActions
{
  public function executeRoute(sfWebRequest $request)
  {
    $router = $this->getRoute();
    $router->doRouting($router->url, $router->context);
  }

And the doRouting() function now forwards directly to the appropriate module/action (correctly taking into account user permissions).

Tom
A: 

I think implementing such a restrictions would be easier to implement as a filter: http://www.symfony-project.org/reference/1_4/en/12-Filters

kuba
I think you're probably right. This way is working fine (as sort of a hacky-filter) so I wont change it, but will bear in mind for next time.
Tom