title: Getting the controller action before behaviour code runs in Yii2
body: <p>I'm trying to execute some code inside a <code>Yii2</code> controller as I need some code from the model to be accessible within the <code>behaviors</code> section so I can pass the model as a parameter and avoid running duplicate queries; however I also need to be able to find out what <code>action</code> is being called, but I am not having much luck.</p>
  
  <p>I have tried using <code>beforeAction</code> but it seems this gets run <strong>AFTER</strong> the <code>behaviours</code> code runs, so that doesn't help me.</p>
  
  <p>I then tried using <code>init</code>, but it seems the <code>action</code> isn't available via <code>$this-&gt;action-&gt;id</code> at that point.</p>
  
  <p>Some example code:</p>
  
  <pre><code>class MyController extends Controller { 
  
      public $defaultAction = 'view';
  
      public function init() {
  
          // $this-&gt;action not available in here
  
      }
  
      public function beforeAction() {
  
          // This is of no use as this runs *after* the 'behaviors' method
  
      }
  
      public function behaviors() {
          return [
              'access' =&gt; [
                  'class' =&gt; NewAccessControl::className(),
                  'only' =&gt; ['view','example1','example2'],
                  'rules' =&gt; [
                      [
                          'allow' =&gt; false,
                          'authManager' =&gt; [
                              'model' =&gt; $this-&gt;model,
                              'other_param' =&gt; $foo,
                              'other_param' =&gt; $bar,
                          ],
                          'actions' =&gt; ['view'],
                      ],
                      // everything else is denied
                  ],
              ],
          ];
      }
  
      public function viewAction() {
  
          // This is how it is currently instantiated, but we want to instantiate *before* the behavior code is run so we don't need to instantiate it twice
          // but to be able to do that we need to know the action so we can pass in the correct scenario
  
          $model = new exampleModel(['scenario' =&gt; 'view']);
  
      }
  
  }
  </code></pre>
  
  <p><code>authManager</code> is simply a reference to a <code>member variable</code> inside an extension of the <code>AccessRule</code> class.</p>
  
  <p>Is there anyway I can do this?</p>