views:

486

answers:

5

I would like to run a Zend Framework action to generate some files, from command line. Is this possible and how much change would I need to make to my existing Web project that is using ZF?

Thanks!

A: 

If you want to run an action that is inside your web project from command line, you can't. The only thing you can run from command line in the Zend Framework is Zend_Tool, which lets you create new actions, controllers, etc.

nandu
Totally untrue.
Laykes
The only Zend Framework component which comes with command line scripting interfaces is Zend_Tool, but you can of course write an application using ZF components that also has a CLI
David Caunt
Out of the box, the only thing you can run on CLI is Zend_Tool. If you code something for CLI using ZF components, is another thing.
nandu
A: 

One option is that you could fudge it by doing a wget on the URL that you use to invoke the desirable action

marsbomber
A: 

You can just use PHP as you would normally from the command line. If you call a script from PHP and either set the action in your script you can then run whatever you want.

It would be quite simple really. Its not really the intended usage, however this is how it could work if you wanted to.

For example

 php script.php 

Read here: http://php.net/manual/en/features.commandline.php

Laykes
A: 

You can use wget command if your OS is Linux. For example:

wget http://example.com/controller/action

See http://linux.about.com/od/commands/l/blcmdl1_wget.htm

UPDATE:

You could write a simple bash script like this:

if wget http://example.com/controller/action
    echo "Hello World!" > /home/wasdownloaded.txt
else
    "crap, wget timed out, let's remove the file."
    rm /home/wasdownloaded.txt
fi

Then you can do in PHP:

if (true === file_exists('/home/wasdownloaded.txt') {
    // to check that the 
}

Hope this helps.

Richard Knop
wget is a possibility, but then your scripts are vulnerable to timeouts
David Caunt
@David You could write a bash script with if else that would save the return value of wget to a file and check with php that that fie exists. But that seems a little overcomplicated, I agree. Nevertheless, I am actually going to update my answer. Maybe it will help.
Richard Knop
+9  A: 

It's actually much easier than you might think. The bootstrap/application components and your existing configs can be reused with CLI scripts, while avoiding the MVC stack and unnecessary weight that is invoked in a HTTP request. This is one advantage to not using wget.

Start your script as your would your public index.php:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH',
              realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV',
              (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                         : 'production'));

require_once 'Zend/Application.php';
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/config.php'
);

//only load resources we need for script, in this case db and mail
$application->getBootstrap()->bootstrap(array('db', 'mail'));

You can then proceed to use ZF resources just as you would in an MVC application:

$db = $application->getBootstrap()->getResource('db');

$row = $db->fetchRow('SELECT * FROM something');

If you wish to add configurable arguments to your CLI script, take a look at Zend_Console_Getopt

If you find that you have common code that you also call in MVC applications, look at wrapping it up in an object and calling that object's methods from both the MVC and the command line applications. This is general good practice.

David Caunt
Thanks for the extensive answer. How would you suggest that I use views in such CLI application? For example, suppose I want to generate a CSV or an XML file through a view? Can I take advantage of the full MCV pattern in this case?
Dario
A Zend_View is really just a templating component, and it can be used in isolation. Literally $view = new Zend_View(); $view->var = 'some data'; and then $view->render('/path/to/script.phtml'); You can also bootstrap a view as normal to set paths/helpers/options and retrieve it from the application. If you wish to simply replace a few details in an XML document, you could use a view. For generating highly dynamic documents, I would just suggest SimpleXML or XMLWriter without a view
David Caunt
+1. Great answer. I didn't see this answer was accepted so I updated my answer.
Richard Knop
Thanks - I've now posted a blog article with roughly the same content at http://www.davidcaunt.co.uk/2010/02/25/easy-command-line-scripts-with-zend-application/
David Caunt