我有一个 ZF2 应用程序从 Web 服务器正常运行。我需要从命令行运行一些操作,因为我想要执行一些计划任务(cron 作业)。
所以我找到了这些有用的链接:Zend框架的官方文档 http://framework.zend.com/manual/2.1/en/modules/zend.console.introduction.html, 萨姆索纳西克的博客 http://samsonasik.wordpress.com/2012/09/05/zend-framework-2-step-by-step-create-console-application/。
我首先在GeneratePdf 模块中的module.config.php 上添加控制台路由。这是控制台路由的一段。
'console' => array(
'router' => array(
'routes' => array(
'generate' => array(
'options' => array(
'route' => 'generate all [--verbose|-v]',
'defaults' => array(
'__NAMESPACE__' => 'GeneratePdf\Controller',
'controller' => 'GeneratePdf',
'action' => 'generateAll'
),
),
),
)
)
),
我在GeneratePdf控制器类上有一个动作,这里是这个动作的段代码:
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();
我没有包含上述操作的所有代码,因为它有很多行。它可以在网络浏览器中正常工作。
为了从控制台运行,我在 ubuntu 13.10 的终端中输入了以下命令:
php public/index.php generate all -v
我的终端上有一堆错误堆栈跟踪:
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
它在index.php文件中的$_SERVER超级全局变量“APPLICATION_ENV”中抛出未定义的索引。我已在可用站点的虚拟主机文件中定义了应用程序环境。它可以在浏览器中正常工作。如果我切换应用程序环境它就可以工作。另一个问题是 PHP 警告:date()。
我在 php.ini 中正确定义了日期。这里是:
date.timezone = Asia/Kathmandu
虽然这不是大问题,但我可以通过在 index.php 上使用此函数来传递日期:
date_default_timezone_set("Asia/Kathmandu");
我还尝试通过以下代码直接在index.php中设置APPLICATION_ENV:
if(!isset($_SERVER['APPLICATION_ENV'])){
$_SERVER['APPLICATION_ENV'] = 'development';
}
当我再次触发此命令时: php public/index.php generated all -v
我在终端没有输出。它没有打印以下文本:“它有效吗?”它位于我的动作文件第一行的 die 函数中。
我对输出完全感到困惑。没有什么可调试的。我的意思是它没有抛出任何错误,那么我该如何调试它。
我完全不知道如何从命令行传递 APPLICATION_ENV 或者可能还有其他选项来设置它。我也尝试在 bashrc 文件中设置路径,但它也不起作用。
我想我已经涵盖了这个问题中所有必需的代码部分/错误。如果有什么遗漏,请告诉我。 :)
谢谢。