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.
On top of the
public/index.php
put: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:
UPDATE: Don't forget to reload your profile file after editing:
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:
The last thing is; write your action name in routing configuration dash-separated notCamelcased: