JamesBarnsley
(Jamesbarnsleyfreelance)
December 10, 2010, 2:30pm
1
Hi,
If I put some variables in my layout file, above all the html start tag, will those variables be accessable in all the application controllers? Kind of like a global variable.
P.S I know global application can be assigned in config, however I was wondering whether this method would work?
mdomba
(Maurizio Domba Cerin)
December 10, 2010, 2:31pm
2
it will not… layout file are rendered as needed…
JamesBarnsley
(Jamesbarnsleyfreelance)
December 10, 2010, 2:42pm
3
I see.
Just to clarify Yii:app()->user is a global variable, normally for storing a user session correct?
So I can obtain the ID of a user from Yii:app()->user->id etc.
Ok now I want to get the users role so I would do something like.
$i = $usermodel->get(Yii:app()->user->id);
echo $i->role;
Where could I put this kinda code $i = $usermodel->get(Yii:app()->user->id); so I can access my user record from anywhere within the application? Also I do not want to store the "user record" in the session, just the ID, however I need the "user record" to be global? So where to put it?
sumwai
(Sumwai Low)
December 10, 2010, 3:29pm
4
JamesBarnsley:
I see.
Just to clarify Yii:app()->user is a global variable, normally for storing a user session correct?
So I can obtain the ID of a user from Yii:app()->user->id etc.
Ok now I want to get the users role so I would do something like.
$i = $usermodel->get(Yii:app()->user->id);
echo $i->role;
Where could I put this kinda code $i = $usermodel->get(Yii:app()->user->id); so I can access my user record from anywhere within the application? Also I do not want to store the "user record" in the session, just the ID, however I need the "user record" to be global? So where to put it?
Maybe you can take a look at the UserIdentity class?
kernel
(Bingotiger)
December 10, 2010, 3:53pm
5
You can extend CWebUser add some methods like getRole()
Your_Class extends CWebUser {
function getRole(){
return $usermodel->get(Yii:app()->user->id);
}
}
Yii:app()->user->getRole();
JamesBarnsley
(Jamesbarnsleyfreelance)
December 10, 2010, 4:00pm
6
kernel:
You can extend CWebUser add some methods like getRole()
Your_Class extends CWebUser {
function getRole(){
return $usermodel->get(Yii:app()->user->id);
}
}
Yii:app()->user->getRole();
Hmm, ok. But would I still be able to access the extended class from Yii:app()->user?
kernel
(Bingotiger)
December 10, 2010, 4:08pm
7
Put Your_Class in an autoload enabled directory E.G /protected/components/Your_Class.php
Then in the protected/config/main.php to configure the user component
set the user ‘class’ to ‘Your_Class’,
'components' => array(
'user' => array(
'class' => 'Your_Class',
…
…
),
Your_Class will be used to initialize the Yii:app()->user there after
jacmoe
(Jacob Moena)
December 10, 2010, 10:20pm
9
@James :
Could you maybe click the vote up button when people give a satisfactory answer?
Then they’ll (we’ll) be more inclined to give some good answers in the future.