isGuest always true

Hi

I have a question, when is Yii::$app->user->isGuest set?

Hi,

Everybody who visits your site is a "guest".

So Yii::$app->user->isGuest is true for all "not logged in" users.

When you login to your app.

You are no guest anymore… you are for example an "registred user" and "isGuest" is false.

Regards

Yea but whats on my mind is this, in yii2\web\Application user is set as a component.

This way you can always get Yii::$app->user->isGuest. But I can’t find where isGuest is set to true or false. Except in yii2\web\User::getIsGuest() givet you your $identity or null. But where does it set $isGuest as it is read-only? Is it through Component::__set()?

Hey,

Start reading the comments in:

app/vendor/yiisoft/yii2/web/User.php

Explaination starts directly at the beginning of the file. ;)

Hope that helps.

Regards

Sorry, after reading your question again my previous answer was maybe not enough.

To explain why it is read-only…

‘isGuest’ is read only, because it is no variable…

It IS actually the getter function "getIsGuest()".

The function determines the 0 or 1 depending on your logged-in status.

Compare it with a function in any of your models.

Lets say your Model is "User.php".

And you build a getter like this:




public function getFullName()

{

   return $this->lastname.", ".$this->firstname;

}



You can call that function later with:

$user->fullName;

But without a setter this getter is "read only"…

And that is exactly what happends with "getIsGuest()".

There is simply no setter.

This is for Yii 1 - but it helps you understanding:

http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/

Look for "Getters and Setters in Detail" …

Regards