title: Runnig Zend Framework 2 action from console not working
body: <p>I have a ZF2 application running from web server properly. I need to run some action from command line because I want to do some scheduled task (cron jobs).</p>
  <p>So I found these useful links:
  <a href="http://framework.zend.com/manual/2.1/en/modules/zend.console.introduction.html" rel="nofollow noreferrer">Zend Framework's official document</a>, <a href="http://samsonasik.wordpress.com/2012/09/05/zend-framework-2-step-by-step-create-console-application/" rel="nofollow noreferrer">Samsonasik's blog</a>.
  I have started by adding console route on module.config.php in GeneratePdf module. Here is the segment of console route.</p>
  <pre><code> 'console' =&gt; array(
      'router' =&gt; array(
          'routes' =&gt; array(
              'generate' =&gt; array(
                  'options' =&gt; array(
                      'route' =&gt; 'generate all [--verbose|-v]',
                      'defaults' =&gt; array(
                          '__NAMESPACE__' =&gt; 'GeneratePdf\Controller',
                          'controller' =&gt; 'GeneratePdf',
                          'action' =&gt; 'generateAll'
                      ),
                  ),
              ),
          )
      )
  ),
  </code></pre>
  <p>I have an action on GeneratePdf controller class, here is the segment code of this action:</p>
  <pre><code>public function generateAllAction() {
      die('Is it working?');
      set_time_limit(150000);
  
      // reading directory
      $d = dir('public/pdf/');
  
      $files = array();
      while (($file = $d-&gt;read()) !== false) {
          $files [] = $file;
      }
      $d-&gt;close();
  </code></pre>
  <p>I have not included all code of the above action because it has lots of lines. It is properly working from web browser.</p>
  <p>To run from console, I entered this command in my ubuntu 13.10's terminal:</p>
  <pre><code>php public/index.php generate all -v
  </code></pre>
  <p>I have got bunch of error stack traces on my terminal:</p>
  <pre><code>PHP Notice:  Undefined index: APPLICATION_ENV in /var/www/zf2-reporting/publi/index.php on line 11
  PHP Stack trace:
  PHP   1. {main}() /var/www/zf2-reporting/public/index.php:0
  PHP Warning:  date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/zf2-reporting/module/Application/Module.php on line 93
  PHP Stack trace:
  PHP   1. {main}() /var/www/zf2-reporting/public/index.php:0
  PHP   2. Zend\Mvc\Application-&gt;run() /var/www/zf2-reporting/public/index.php:32
  PHP   3. Zend\EventManager\EventManager-&gt;trigger() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:290
  PHP   4. Zend\EventManager\EventManager-&gt;triggerListeners() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207
  PHP   5. call_user_func() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468
  PHP   6. BjyAuthorize\Guard\Controller-&gt;onDispatch() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468
  PHP   7. Zend\EventManager\EventManager-&gt;trigger() /var/www/zf2-reporting/module/BjyAuthorize/src/BjyAuthorize/Guard/Controller.php:176
  PHP   8. Zend\EventManager\EventManager-&gt;triggerListeners() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207
  PHP   9. call_user_func() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468
  PHP  10. Application\Module-&gt;Application\{closure}() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468
  PHP  11. Zend\ServiceManager\ServiceManager-&gt;get() /var/www/zf2-reporting/module/Application/Module.php:44
  PHP  12. Zend\ServiceManager\ServiceManager-&gt;create() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:480
  PHP  13. Zend\ServiceManager\ServiceManager-&gt;doCreate() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:556
  PHP  14. Zend\ServiceManager\ServiceManager-&gt;createFromFactory() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:597
  PHP  15. Zend\ServiceManager\ServiceManager-&gt;createServiceViaCallback() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:984
  PHP  16. call_user_func() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:852
  PHP  17. Application\Module-&gt;Application\{closure}() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:852
  PHP  18. date() /var/www/zf2-reporting/module/Application/Module.php:93
  PHP Fatal error:  Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/Log/Logger.php:399
  Stack trace:
  #0 /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/Log/Logger.php(399): DateTime-&gt;__construct()
  #1 /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/Log/Logger.php(451): Zend\Log\Logger-&gt;log(2, Object(BjyAuthorize\Exception\UnAuthorizedException), Array)
  #2 /var/www/zf2-reporting/module/Application/Module.php(44): Zend\Log\Logger-&gt;crit(Object(BjyAuthorize\Exception\UnAuthorizedException))
  #3 [internal function]: Applicat in /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/Log/Logger.php on line 399
  </code></pre>
  <p>It is throwing undefined index in $_SERVER super global variable &quot;APPLICATION_ENV&quot; in index.php file. I have defined application environment in my available site's virtual host file. It is properly working from the browser. If I switch the application environment it works. Another issue is PHP Warning:  date().</p>
  <p>I have properly defined date in my php.ini. Here it is:</p>
  <pre><code>date.timezone = Asia/Kathmandu
  </code></pre>
  <p>Though it is not big issue, I can pass date by using this function on index.php:</p>
  <pre><code>date_default_timezone_set(&quot;Asia/Kathmandu&quot;);
  </code></pre>
  <p>I have also tried setting APPLICATION_ENV directly in index.php by following piece of code:</p>
  <pre><code>if(!isset($_SERVER['APPLICATION_ENV'])){
      $_SERVER['APPLICATION_ENV'] = 'development';
  }
  </code></pre>
  <p>When I again fire this command: php public/index.php generate all -v</p>
  <p>I got no output in terminal. And it is not printing this text: &quot;Is it working?&quot; which is in the die function of first line of my action file.
  I am totally confused from the output. There is nothing to debug. I mean it is not throwing any error so how can I debug it.</p>
  <p>I am totally unaware how to pass APPLICATION_ENV from command line or there may be other options to set it. I also tried by setting path in bashrc file but it is not working too.</p>
  <p>I think I have covered all the required code sections/ error in this question. If there is anything missing, please kindly tell me. :)</p>
  <p>Thank you.</p>