title: Yii2 check if user is loggedin in yii2
body: <p>I have a controller action that i would like to check if user is a guest in yii2 but the code fails even if the user is a guest this is what i have tried</p>
  
  <pre><code>class CustomerController extends Controller
   {
  
     public function beforeAction($action)
      {
      if(Yii::$app-&gt;user-&gt;isGuest){
          return $this-&gt;redirect(Yii::$app-&gt;urlManager-&gt;createUrl("site/login"));
      }
  
  
     public function actionDashboard(){
      //do dashboard stuff
     }
  
    }
  </code></pre>
  
  <p>I have also tried using accessControl filters but they still fail
  This is the access control filter instance in a Frontend Controller</p>
  
  <pre><code>public function behaviors()
  {
      return [
          'access' =&gt; [
              'class' =&gt; AccessControl::className(),
              'rules' =&gt; [
                  [
                      'controllers' =&gt; ['customer'],
                      'actions' =&gt; ['dashboard'],
                      'allow' =&gt; true,
                      'users' =&gt; ['?'],
                  ],
  
              ], // rules
  
          ], // access
  
  
      ]; // return
  
  } // behaviors
  </code></pre>
  
  <p>Then on my customer controller i just extend Frontend Controlller but even with this approach it fails</p>
  
  <p>BY DOING IT THIS WAY</p>
  
  <pre><code>public function actionDashboard(){
      if(Yii::$app-&gt;user-&gt;isGuest){
          return $this-&gt;redirect(Yii::$app-&gt;urlManager-&gt;createUrl("site/login"));
      }else{
  
        //perform dashboard stuff
  
      }
  
  }
  </code></pre>
  
  <p>This works but the problem is that by following the last approach there is alot of code duplication in the other actions which doesnt seem right even though it works</p>
  
  <p>What is wrong with the latter approaches which seem better</p>
  
  <p>I have checked on <a href="https://stackoverflow.com/questions/30898214/yii2-check-if-the-user-is-logged-in-view">This link</a> and also <a href="https://stackoverflow.com/questions/25998122/yii2-global-filter-behavior-to-force-user-to-authenticate-first">This second one</a> and implementend them but none works</p>
  
  <p>WHAT COULD BE WRONG. </p>
  
  <pre><code>AM using Yii2 improved advanced template
  </code></pre>