从控制台运行Zend Framework 2操作无法正常工作

时间:2014-01-04 15:27:24

标签: php console zend-framework2

我有一个从Web服务器正常运行的ZF2应用程序。我需要从命令行运行一些操作,因为我想做一些计划任务(cron作业)。

所以我找到了这些有用的链接: Zend Framework's official documentSamsonasik's blog。 我已经开始在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生成所有-v

终端没有输出。它不打印这个文字:“它有效吗?”这是我的动作文件第一行的die函数。 我对输出感到困惑。没有什么可以调试的。我的意思是它不会抛出任何错误,所以我该如何调试它。

我完全不知道如何从命令行传递APPLICATION_ENV,或者可能还有其他选项来设置它。我也试过在bashrc文件中设置路径,但它也不起作用。

我想我已经涵盖了这个问题中所有必需的代码部分/错误。如果有任何遗漏,请告诉我。 :)

谢谢。

2 个答案:

答案 0 :(得分:3)

从控制台运行应用程序时,虚拟主机不在阶段,因为它不是HTTP请求。这是一个CLI请求。

在〜/ .bashrc文件中定义您的APPLICATION_ENV变量,如下所示:

export APPLICATION_ENV="development"

更新:编辑后不要忘记重新加载配置文件:

source ~/.bashrc

此外,某些系统(如ubuntu)使用不同的php.ini文件进行CLI。例如,在我的个人服务器上,我有两个php.ini文件,如下所示:

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

最后一件事是;在路由配置中写下你的动作名称,以破折号分隔的notCamelcased:

'action' => 'generateAll'  // WRONG
'action' => 'generate-all' // CORRECT

答案 1 :(得分:-1)

public/index.php put:

之上
if (! getenv('APPLICATION_ENV')) {
    putenv('APPLICATION_ENV=dev');
}