Variables

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?

it will not… layout file are rendered as needed…

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?

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?

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

Cheers.

@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.