I’m having difficulty understanding what’s going on here
class AppController extends Controller {
...
public function beforeFilter() {
$user = $this->Auth->user();
if (!empty($user)) {
Configure::write('User', $user[$this->Auth->getModel()->alias]);
}
}
...
}
In my example…
-
$user = Array ( [id] => 1 [username] => user [active] => 1 [email] => user@site.com )
-
$this->Auth->getModel()->alias = ‘User’ (string)
-
$user[$this->Auth->getModel()->alias] = Array ( [id] => 1 [username] => user [active] => 1 [email] => user@site.com )
My questions:
-
How are $user[‘User’] and $user the same thing? It looks to me $user is an associative array with keys id, username, active, and email. I’m not sure how using the key ‘User’ works.
-
Could someone explain the “::” syntax and what Configure::write(‘User’, …) does and how to access this ‘User’ variable (probably has some conventional term for it..). I’m finding that Configure::read(‘User’) retrieve this information. Is this right?
-
Finally.. I’m confused between two terms User (model as in $this->User->create() to create a new user) and User (from Configure::write(‘User’,…)). They are not the same things, are they?
You can use different models together with the AuthComponent. The default is
User, but you may make itMember,Adminor whatever else your user models are called. Calling$this->Auth->user()returns an array with that primary name attached to it, like:(I suspect you’re debugging your
$uservariable wrong there.)The only thing that block of code does is to write the user details into the Configuration (just a place to store values) without this intermediate
Adminkey. I guess this is so any piece of code anywhere can get at the current user details without having to worry about whether it’s$user['User']or$user['Admin']or something else. Instead you can simply useConfigure::read('User')orConfigure::read('User.id')orConfigure::read('User.name')etc.The choice of the name
UserinConfigure::write('User', ...)is entirely arbitrary. You could use anything there, it has no predefined meaning.BTW, if it’s just the controller, you can get the details of the current user without worrying about what the user model is called using
$this->Auth->user('name')(to get the name, for example).