Joel Grannas
Joel Grannas

Reputation: 2016

writing output to a file with CakePHP shell

I am trying to just simply write "hello world" to a file, from a cakephp shell, with plans to eventually write a sitemap.xml file using our product models. I found this: Question which got me started...

But i am thinking either ConsoleOutput is not supported in Cake 1.3.6 (which i'm using), or i need to include the class that holds it.

The error i get when trying to run the file from terminal: PHP Fatal error: Class 'ConsoleOutput' not found in /public/vendors/shells/sitemap.php on line 7

Here is my code:

class SitemapShell extends Shell {

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

}

Upvotes: 0

Views: 2152

Answers (1)

toby1kenobi
toby1kenobi

Reputation: 1701

You are correct that ConsoleOutput didn't feature in CakePHP 1.3 - can you upgrade to a version 2.*?

If not you could just use regular PHP:

$fp = fopen('hello.txt', 'w');
fwrite($fp, 'hello world');
fclose($fp);

Hope this helps.

Toby

Upvotes: 1

Related Questions