Runnig Zend Framework 2操作从控制台不工作 [英] Runnig Zend Framework 2 action from console not working

查看:352
本文介绍了Runnig Zend Framework 2操作从控制台不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以我发现了这些有用的链接:
Zend Framework的官方文档 Samsonasik的博客
我开始通过在GeneratePdf模块中的module.config.php添加控制台路由。这是控制台路由段。

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'
                    ),
                ),
            ),
        )
    )
),



我对GeneratePdf控制器类,这里是这个动作的段代码:

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.

要从控制台运行,我在ubuntu 13.10的终端输入了这个命令:

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

它是在index.php文件中的$ _SERVER超级全局变量APPLICATION_ENV中抛出未定义的索引。我已经在我可用的站点的虚拟主机文件中定义了应用程序环境。它从浏览器正常工作。如果我切换应用程序环境它工作。另一个问题是PHP警告:date()。

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

我在我的php.ini中正确定义了日期。这是:

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

date.timezone = Asia/Kathmandu

虽然这不是大问题,我可以通过使用index.php上的这个函数传递日期:

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

date_default_timezone_set("Asia/Kathmandu");

我还尝试通过以下代码直接在index.php中设置APPLICATION_ENV:

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';
}


$ b < v

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

我在终端没有输出。它不打印这段文字:它工作吗?这是在我的行动文件的第一行的die函数。
我完全困惑于输出。没有什么可以调试。我的意思是它不会抛出任何错误,所以我该如何调试它。

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.

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

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

谢谢。

推荐答案

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

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

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

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

export APPLICATION_ENV="development"

UPDATE: / strong>编辑后不要忘记重新加载您的个人资料文件:

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

source ~/.bashrc

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

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

最后一件事是在路由配置中写入您的操作名称dash-separated notCamelcased:

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

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

这篇关于Runnig Zend Framework 2操作从控制台不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆