2

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).

So I found these useful links: Zend Framework's official document, Samsonasik's blog. I have started by adding console route on module.config.php in GeneratePdf module. Here is the segment of console route.

 'console' => array(
    'router' => array(
        'routes' => array(
            'generate' => array(
                'options' => array(
                    'route' => 'generate all [--verbose|-v]',
                    'defaults' => array(
                        '__NAMESPACE__' => 'GeneratePdf\Controller',
                        'controller' => 'GeneratePdf',
                        'action' => 'generateAll'
                    ),
                ),
            ),
        )
    )
),

I have an action on GeneratePdf controller class, here is the segment code of this action:

public function generateAllAction() {
    die('Is it working?');
    set_time_limit(150000);

    // reading directory
    $d = dir('public/pdf/');

    $files = array();
    while (($file = $d->read()) !== false) {
        $files [] = $file;
    }
    $d->close();

I have not included all code of the above action because it has lots of lines. It is properly working from web browser.

To run from console, I entered this command in my ubuntu 13.10's terminal:

php public/index.php generate all -v

I have got bunch of error stack traces on my terminal:

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->run() /var/www/zf2-reporting/public/index.php:32
PHP   3. Zend\EventManager\EventManager->trigger() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:290
PHP   4. Zend\EventManager\EventManager->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->onDispatch() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468
PHP   7. Zend\EventManager\EventManager->trigger() /var/www/zf2-reporting/module/BjyAuthorize/src/BjyAuthorize/Guard/Controller.php:176
PHP   8. Zend\EventManager\EventManager->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->Application\{closure}() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468
PHP  11. Zend\ServiceManager\ServiceManager->get() /var/www/zf2-reporting/module/Application/Module.php:44
PHP  12. Zend\ServiceManager\ServiceManager->create() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:480
PHP  13. Zend\ServiceManager\ServiceManager->doCreate() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:556
PHP  14. Zend\ServiceManager\ServiceManager->createFromFactory() /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:597
PHP  15. Zend\ServiceManager\ServiceManager->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->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->__construct()
#1 /var/www/zf2-reporting/vendor/zendframework/zendframework/library/Zend/Log/Logger.php(451): Zend\Log\Logger->log(2, Object(BjyAuthorize\Exception\UnAuthorizedException), Array)
#2 /var/www/zf2-reporting/module/Application/Module.php(44): Zend\Log\Logger->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

It is throwing undefined index in $_SERVER super global variable "APPLICATION_ENV" 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().

I have properly defined date in my php.ini. Here it is:

date.timezone = Asia/Kathmandu

Though it is not big issue, I can pass date by using this function on index.php:

date_default_timezone_set("Asia/Kathmandu");

I have also tried setting APPLICATION_ENV directly in index.php by following piece of code:

if(!isset($_SERVER['APPLICATION_ENV'])){
    $_SERVER['APPLICATION_ENV'] = 'development';
}

When I again fire this command: php public/index.php generate all -v

I got no output in terminal. And it is not printing this text: "Is it working?" 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.

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.

I think I have covered all the required code sections/ error in this question. If there is anything missing, please kindly tell me. :)

Thank you.

Community
  • 1
  • 1
regeint
  • 888
  • 2
  • 17
  • 38

2 Answers2

4

Virtual host is not in stage when you run your app from console, because its not an HTTP request. It's a CLI request.

Define your APPLICATION_ENV variable in your ~/.bashrc file something like this:

export APPLICATION_ENV="development"

UPDATE: Don't forget to reload your profile file after editing:

source ~/.bashrc

Also, some systems (like ubuntu) uses different php.ini file for CLI. For example, on my personal server i have two php.ini files like follows:

/etc/php5/fpm/php.ini 
/etc/php5/cli/php.ini // This is CLI

The last thing is; write your action name in routing configuration dash-separated notCamelcased:

'action' => 'generateAll'  // WRONG
'action' => 'generate-all' // CORRECT
edigu
  • 9,878
  • 5
  • 57
  • 80
  • Hi foozy, I have exported APPLICATION_ENV, set timezone in cli php.ini and the last one: 'action' => 'generate-all'. Those warning has gone away but still it is showing empty output. Are there any other thing I need to check into, please provide me. Thanks – regeint Jan 05 '14 at 03:12
  • What you mean by empty output? Is your generateAllAction dispatching? – edigu Jan 05 '14 at 22:53
  • The process is not reaching generateAllAction form cli mode, however is can be reached from web browser. – regeint Jan 06 '14 at 11:20
  • I think problem is in routing configuration. Try `'controller' => 'Application\Controller\GeneratePdf'` instead of `'controller' => 'GeneratePdf'` in routing configuration. This should work. Also change namespace from `'__NAMESPACE__' => 'GeneratePdf\Controller'` to `'__NAMESPACE__' => 'Application\Controller'` if GeneratePdf is not a module (top-level namespace). – edigu Jan 06 '14 at 12:14
  • I have 'GeneratePdf' module so I edited module.config.php by this: '__NAMESPACE__' => 'GeneratePdf\Controller\GeneratePdf' and now it is throwing error. I have used ZfcUser and BjyAuthorize to authenticate. It is throwing exception: 'You are not authorized to access GeneratePdf\Controller\GeneratePdf\GeneratePdf:generate-all' in /var/www/zf2-reporting/module/BjyAuthorize/src/BjyAuthorize/Guard/Controller.php:172 – regeint Jan 06 '14 at 12:32
  • Oh, nice! Now your main problem is solved. All you need is correctly configure BjyAuthorize module by your requirements. – edigu Jan 06 '14 at 13:33
  • Thanks @foozy, I have solve remaining issue in this question: http://stackoverflow.com/questions/20990416/give-permission-for-bjyauthorize-to-run-mvc-application-of-zf2-from-cli – regeint Jan 15 '14 at 10:30
-1

On top of the public/index.php put:

if (! getenv('APPLICATION_ENV')) {
    putenv('APPLICATION_ENV=dev');
}
kenorb
  • 155,785
  • 88
  • 678
  • 743
Mike Doe
  • 16,349
  • 11
  • 65
  • 88