tags:

views:

36

answers:

3

Hello,

I want to access

  • id of current logged user
  • name of current logged user
  • group_id of current logged user
  • group_name of current logged user

across the view files, to switch menus and tabs on and off according to group_id.

How can I achieve this with minimum sacrifice of performance?

Thanks

+1  A: 

Perhaps you could set the variables you want in your AppController (extended by all sub controllers). You should then be able to access them from all views, though be careful to name them uniquely. CakePHP book - App Controller

danielgwood
+4  A: 

If you use the AuthComponent, it'll store the record of the currently logged-in user in the Session under the key Auth. You can access this anywhere through the session component or helper:

$this->Session->read('Auth.User.name')

Even if you're not using the AuthComponent, the Session is the best place to store information about the current user.

Otherwise and in general, the Configure class is usually a good place to store this kind of global information:

Configure::write('User', array('id' => $id, ...));

Configure::read('User.id');
deceze
ah, I absolutely forgot the session :) Thanks Deceze
Moe Sweet
+2  A: 

What I always do is create a AppHelper and create a method for this. Off course this is similar to deceze's answer but it reduces some code you need to write ;)

function user($key) {
   $user = $this->Session->read('Auth.User');
    if (isset($user[$key])) {
     return $user[$key];
    }
   return false;
 }  

Then you can call the id of the user by $this->Html->user('id');

Laurent
I don't fully understand by the fragment of code. Sorry :( Thanks for the answer anyway.
Moe Sweet
what part don't you understand? :)
Laurent