Selasa, 26 Oktober 2010

CakePHP: Access user data practically from anywhere

I just stumble upon this idea when preparing CakePHP Training materials for MOSTI, Putrajaya. Imagine that you're a seasoned Cake Baker and you would want to get access of logged in user in

1. Models
2. Controllers
3. Views
4. Components
5. Helpers

using the same simple call .

So here's how I did it. First, after the User logged in, grab the user's ID and use CakePHP Magic call to fetch all the user's details and later store it in Configure's basket.

app_controller.php

Now here the best part,

# fetch the user's id
$user_id = Configure::read('LoggedIn.User.id');

# fetch the user's email
$user_email = Configure::read('LoggedIn.User.email');

# fetch the user's group title ( User belongsTo Group )
$user_group_title = Configure::read('LoggedIn.User.Group.title');
And the best part is, you can use this call in Model,Behavior,Helper,Component,View,Element and Controller.


getting $user_id in Model Callback never been easier

Tiada ulasan:

Catat Ulasan