title: Trying to get property of non-object on Yii2
body: <p>I want to show data based on user's group id, which is i use</p>
  
  <pre><code>Yii::$app-&gt;user-&gt;identity-&gt;group_id
  </code></pre>
  
  <p>in my ModelSearch like this :</p>
  
  <pre><code>$query-&gt;andFilterWhere([
          ......
          'group_id' =&gt; Yii::$app-&gt;user-&gt;identity-&gt;group_id,
          'created_date' =&gt; $this-&gt;dreated_date,
      ]);
  </code></pre>
  
  <p>It works when i have session in the application, but when i don't have session the page show error :</p>
  
  <pre><code>Trying to get property of non-object
  </code></pre>
  
  <p>For  <code>'group_id' =&gt; Yii::$app-&gt;user-&gt;identity-&gt;group_id,</code></p>
  
  <p>How to make the page redirect to login page when i open that page if I don't have any session. Thank You.</p>
  
  <p>EDIT</p>
  
  <p>I added         </p>
  
  <pre><code>if (Yii::$app-&gt;user-&gt;isGuest) {
              return $this-&gt;redirect(Yii::$app-&gt;user-&gt;loginUrl);
  }
  </code></pre>
  
  <p>in every action in my contoller that access ModelSearch .</p>