title: place common variables and functions to use across controllers in zf2
body: <p>What is the best/standard way to put common variables and functions in Zend framework 2 (with doctrine), to be used across all the modules, specifically their controllers.</p>
  
  <p>I read somewhere that our controllers should extend another controller (like AppCommonController) which, in turn, extends AbstractActionController. The AppCommonController will then define the common variables and functions that we can access in any controller that extends it.</p>
  
  <p>Is there a better/standard way to do this?</p>
  
  <p>---Updated---</p>
  
  <p>Say for e.g., I want to check the current mode of my site (test or live) in most of my controllers (across different modules), and accordingly want to do the necessary in the actions.</p>
  
  <p>I write following in some controller:</p>
  
  <pre><code>    private $__currentMode = '';
  
      public function __construct()
      {
          //following will be set to Live or Test depending on a session value
          $this-&gt;setCurrentMode('Live');
      }
  
      public function setCurrentMode($mode)
      {
          $this-&gt;__currentMode = $mode;
      }
  
      public function getCurrentMode()
      {
          return $this-&gt;__currentMode;
      }
  </code></pre>
  
  <p>I believe it is a bad idea to put above code in all the controllers where I need to check the current mode. </p>
  
  <p>So I want to put it (both the currentMode property and getter/setter functions) at some place from where I can access them in all the controllers wherever needed.</p>