从控制台运行 Zend Framework 2 操作不起作用

2024-01-10

我有一个 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 文件中设置路径,但它也不起作用。

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

谢谢。


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

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

export APPLICATION_ENV="development"

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

source ~/.bashrc

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

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

最后一件事是;在路由配置中写入您的操作名称,以破折号分隔,而不是驼峰式:

'action' => 'generateAll'  // WRONG
'action' => 'generate-all' // CORRECT
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从控制台运行 Zend Framework 2 操作不起作用 的相关文章

  • 使用 Laravel 4 验证多个文件上传

    如何在 Laravel 4 中验证上传文件的数组 我已将其设置为允许多个文件 并且已测试这些文件是否存在于 Input file files 数组中 但如何验证每个文件呢 这是我尝试过的 notesData array date gt In
  • 如何使用 PHP 正确添加跨站请求伪造 (CSRF) 令牌

    我正在尝试为我网站上的表单添加一些安全性 其中一个表单使用 AJAX 另一个表单是简单的 联系我们 表单 我正在尝试添加 CSRF 令牌 我遇到的问题是令牌有时只显示在 HTML 值 中 其余时间 该值为空 这是我在 AJAX 表单上使用的
  • 简单的 PHP 条件帮助: if($Var1 = in list($List) and $Cond2) - 这可能吗?

    这是一个可能的功能吗 我需要检查一个变量是否存在于我需要检查的变量列表中 并且 cond2 是否为 true 例如 if row name 1 2 3 Cond2 doThis 它对我不起作用 我在复制粘贴中更改的只是我的列表和变量名称 i
  • Laravel 从 5.6 升级到 Laravel 6

    我有一个项目https github com javedbaloch4 Laravel Booking https github com javedbaloch4 Laravel Booking发展于Laravel 5 6现在我想将其升级到
  • 删除PHP字符串中所有不匹配的字符?

    我有一个文本 我想从中删除所有不属于以下字符的字符 所需字符 0123456789 abcdefghijklmnopqrstuvwxyz n 最后一个是我确实想保留的 n 换行符 要匹配除列出的字符之外的所有字符 请使用反转字符集 http
  • 如何解压 PHP/Lumen/Laravel 的 gzip 请求?

    我收到来自第三方的 gzip 编码文本请求 1mb 所以这是有道理的 我的测试路线 router gt post testgzip function Illuminate Http Request request decompressed
  • 生成大随机数 php [重复]

    这个问题在这里已经有答案了 我想使用 PHP 生成一个包含 75 个字符的数字 我到处寻找 但一无所获 除了这个 http dailycoding com tools RandomNumber aspx http dailycoding c
  • 如何在响应ajax codeigniter后停止执行其他控制器

    我想知道如何在响应输出 json 数据后停止执行函数和涉及的其他控制器 就我这里的情况而言 我只是打电话test 函数于dashboard控制器 In dashboard构造函数将执行MY Login library In MY Login
  • 如何从父类函数访问子类中定义的常量?

    我从 php net 看到这个例子 但 c MY CONST 仅在 5 3
  • 蛋糕控制台 2.2.1:烘焙错误

    运行 MAMP 的 OSX 机器 CakePHP 2 2 1 已正确安装和配置 这意味着当我浏览到 Index php 文件时 所有绿色条都显示出来 我已经完成了博客教程 并且正在开发我的第二个应用程序 其中脚手架已启动并运行 现在我第一次
  • Symfony2 Assetic 和 Less Sourcemaps

    我不确定如何破解 assetic less 过滤器以输出源映射文件 我这里指的是LessFilterhttps github com kriswallsmith assetic blob master src Assetic Filter
  • preg_match_all 查询仅显示有问题的外部组

    我无法弄清楚如何只显示 preg 查询的外部组级别 我会给你一个例子 preg match all start end input matches 这个输入start1 start2 2end 1end产生这个输出start1 start2
  • “使用未定义常量”注意,但该常量应该被定义

    共有三个文件 common php controller php 和 user php 文件 common php 如下所示 文件controller php看起来像 文件 user php 如下所示 执行脚本时 会给出通知 注意 使用未定
  • 在 PHP 中撤销 Google 访问令牌

    正如标题所示 我想以编程方式撤销授予的访问令牌 即在 PHP 中 我发现这个他们的网站 https developers google com identity protocols OAuth2WebServer tokenrevoke 但
  • 在 PHP 中使用 phpseclib 时出现 RSA 问题

    我正在尝试在 phpseclib 中使用 RSA 实现 我认为在函数中执行一次代码并重新使用该函数会更容易 当我尝试向代码发送短信时 我收到一条错误消息 提示 解密错误 测试还让我意识到每次代码运行时密文都是不同的 所以我显然在那里做错了什
  • PHP 中只保留数组的前 N ​​个元素? [复制]

    这个问题在这里已经有答案了 有没有办法只保留数组的前 N 个 例如 10 个 元素 我知道有array pop 但是有没有更好 更优雅的方法呢 您可以使用array slice http php net array slice or arr
  • SQL 最近日期

    我需要在 php 中获取诸如 2010 04 27 之类的日期作为字符串 并在表中找到最近的 5 个日期 表中的日期保存为日期类型 您可以使用DATEDIFF http dev mysql com doc refman 5 1 en dat
  • 为什么 Composer 降级了我的包?

    php composer phar update这样做了 删除了 2 3 0 软件包并安装了整个 2 2 5 Zend Framework php composer phar update Loading composer reposito
  • post php mysql 的拆分关键字

    我有一个表存储帖子 ID 它的标签如下 Post id Tags 1 keyword1 keyword2 keyword3 我想循环遍历该表中的每一行并执行以下操作 将关键字1 关键字2 关键字3放入新表中 word id word val
  • 无法显示 Laravel 欢迎页面

    我的服务器位于 DigitalOcean 云上 我正在使用 Ubuntu 和 Apache Web 服务器 我的家用计算机运行的是 Windows 7 我使用 putty 作为终端 遵循所有指示https laracasts com ser

随机推荐