T
T
Tokenchik2017-05-16 08:15:49
Yii
Tokenchik, 2017-05-16 08:15:49

How to check the existence of an object in Yii2?

There is a small function -

function adminvalidate() {
        if ( Yii::$app->user->identity->username != 'admin' ) {
            return Yii::$app->response->redirect(['/site/login', 'id' => $id]);
        }
    }

Everything works in the locale, if the function is called on the hosting, an error occurs -
PHP Notice - yii \ base \ ErrorException
Trying to get property of non-object
As I understand it, inside the function, you must first check whether the username exists at all in identity, if so, then we call check. What is the best way to do this?
PS Do not throw slippers, I know about RBAC, but the site is too small and there is a lack of experience and time)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Screamy Di, 2017-05-16
@Tokenchik

You can check with Yii::$app->user->isGuest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question