cakephp - 如何捕获 CakePHP Shell 的输出

标签 cakephp cakephp-2.0

有什么方法可以在 CakePHP 中捕获 shell 的输出吗?

我编写了一些 shell,可以为 CakePHP 2.x 应用程序生成报告。我能够在命令行上运行 shell 并查看输出,但是,现在我想通过电子邮件发送这些 shell 的结果。

我考虑过使用另一个 shell 作为包装器,然后使用 $this->dispatchShell('shellname') 来捕获它的输出,但似乎 dispatchShell 只是运行shell 并将其输出转储到 CLI。

最佳答案

要将 Shell 输出到文件,请在 Shell 的构造函数中声明一个输出流。下面是一个将标准输出作为日志文件的示例,该文件位于 CakePHP 的 TMP 目录(通常是 app/tmp/)中名为 shell.out 的文件中:

<?php
class FooShell extends AppShell {

    public function __construct($stdout = null, $stderr = null, $stdin = null) {
        // This will cause all Shell outputs, eg. from $this->out(), to be written to
        // TMP.'shell.out'
        $stdout = new ConsoleOutput('file://'.TMP.'shell.out');

        // You can do the same for stderr too if you wish
        // $stderr = new ConsoleOutput('file://'.TMP.'shell.err');

        parent::__construct($stdout, $stderr, $stdin);
    }

    public function main() {
        // The following output will not be printed on your console
        // but be written to TMP.'shell.out'
        $this->out('Hello world');
    }
}

关于cakephp - 如何捕获 CakePHP Shell 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11462829/

相关文章:

cakephp - Cake PhP block - 如何使用它们?

php - 如何测试是否从 CakePHP 控制台运行?

html - Bootstrap 网格和布局输出

php - 如何在cakephp中更新记录

php - 如何在 CakePHP save() 函数中使用 MySQL 函数

php - 将文本区域保存到数据库并发送电子邮件,保持文本区域格式

cakephp - 从 Cake 1.3 迁移到 2.0 及更高版本 - 迁移现有的,还是仅用于新的?

php - CakePHP 日期选择的默认值

cakephp - 生产中使用 facebook sdk 的 cakephp 2.x 中的 session 问题

php - 如何在 CakePHP 3 中使用 : Upload multiple images to server and save each image in database using cakephp 3