【问题标题】:Runnig Zend Framework 2 action from console not working从控制台运行 Zend Framework 2 操作不起作用
【发布时间】:2014-01-22 06:05:59
【问题描述】:

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

所以我找到了这些有用的链接: Zend Framework's official document,Samsonasik'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 generate all -v

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

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

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

谢谢。

【问题讨论】:

    标签: php console zend-framework2


    【解决方案1】:

    当您从控制台运行应用程序时,虚拟主机不在阶段,因为它不是 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
    

    【讨论】:

    • 嗨傻瓜,我已经导出了 APPLICATION_ENV,在 cli php.ini 和最后一个设置时区:'action' => 'generate-all'。这些警告已经消失,但仍然显示空输出。还有其他需要检查的东西吗,请提供给我。谢谢
    • 空输出是什么意思?你的 generateAllAction 调度了吗?
    • 进程未达到 generateAllAction form cli 模式,但可以从 Web 浏览器访问。
    • 我认为问题在于路由配置。在路由配置中尝试'controller' => 'Application\Controller\GeneratePdf' 而不是'controller' => 'GeneratePdf'。这应该有效。如果 GeneratePdf 不是模块(顶级命名空间),还将命名空间从 '__NAMESPACE__' => 'GeneratePdf\Controller' 更改为 '__NAMESPACE__' => 'Application\Controller'。
    • 我有 'GeneratePdf' 模块,所以我通过以下方式编辑了 module.config.php:'NAMESPACE' => 'GeneratePdf\Controller\GeneratePdf' 现在它抛出错误.我已经使用 ZfcUser 和 BjyAuthorize 进行身份验证。它抛出异常:'您无权访问 /var/www/zf2-reporting/module/BjyAuthorize/src/BjyAuthorize/Guard/Controller.php:172 中的 GeneratePdf\Controller\GeneratePdf\GeneratePdf:generate-all' /跨度>
    【解决方案2】:

    在public/index.php 之上放置:

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

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多