title: Yii2 global filter/behavior to force user to authenticate first
body: <p>In my Yii2 application I'm trying to force all users to be authenticated. If they're not already authenticated they should be redirected to the login page.</p>
  
  <p>In Yii1 I did this by creating a class that would check if a user was logged in and attaching that class to the <code>onBeginRequest</code> behavior in my main config file.</p>
  
  <pre><code>// Yii 1
  'behaviors' =&gt; array(
      'onBeginRequest' =&gt; array(
          'class' =&gt; 'application.components.RequireLogin',
      )
  ),
  </code></pre>
  
  <p>How can I get the same behavior in Yii2? I know I can use behavior to do this, but I wan't to add this behavior to my main config file so all requests are first checked for authentication.</p>
  
  <p>The working behaviors method looks like this:</p>
  
  <pre><code>// Yii2
  public function behaviors() {
      return [
          'access' =&gt; [
              'class' =&gt; AccessControl::className(),
              'rules' =&gt; [
                  [
                      'actions' =&gt; ['login', 'error'],
                      'allow' =&gt; true,
                  ],
                  [
                      'allow' =&gt; true,
                      'roles' =&gt; ['@'],
                  ],
              ],
          ],
      ];
  }
  </code></pre>