Anshuman Jasrotia
Anshuman Jasrotia

Reputation: 3185

Yii :: Use of undefined constant

I am using the below code in the accessRules() function to match the roles of the logged in user. The role is set during the authentication process using

 $this->setState('role', $record->role);

The code in the accessRules() function is:

    $role="";
    if(Yii::app()->user->getId()===null)
    {
            $role="guest";
    }
    else
    {
        $role=Yii::app()->user->role;
    }

   return array(
        array('allow', 'actions'=>array('index','view'),
            'users'=>array('*'),
        ),
   );

This code is working fine on my local machine but when I upload it to the server it gives me Use of undefined constant guest - assumed 'guest' error. What is the reason for this and how can I resolve this. Thanks!!!

Upvotes: 0

Views: 4420

Answers (2)

Anshuman Jasrotia
Anshuman Jasrotia

Reputation: 3185

Followed this post and changed the authorization process and it worked. Still not able to figure out why this warning was shown.

Upvotes: 0

Samuel Liew
Samuel Liew

Reputation: 79073

$role="";
if(Yii::app()->user->getId()===null)
{
        $role="guest";
}
else
{
    $role=Yii::app()->user->role;
}

can be simplified to:

$role = "guest";
if(Yii::app()->user->id != null) {
    $role = Yii::app()->user->role;
}

Try and see if this solves the problem for you.

You can also do this:

$role = "guest";
if(!Yii::app()->user->isGuest) {
    $role = Yii::app()->user->role;
}

However, it seems you might be using the variable $role elsewhere, you may want to see if those lines are causing the problem instead.

Upvotes: 1

Related Questions