views:

40

answers:

2

I need to create a script that will run on the command line using PHP and I want to take advantage of the ZF and the models (classes) I have written using it. How do I do this as elegantly as possible?

+1  A: 

Basically, a CLI interface is just a different presentation layer. If you kept the separation of your M vs VC clean, all you need is a new entry point to address the Model, e.g your CLI interface.

You can use Zend_Console_Getopt to ease development of the CLI client. It allows you quickly parse input passed to a CLI script. You will have to delegate any input to your Model then, just like you would "regularly".

Gordon
+1  A: 

You have to duplicate the code of public/index.php without calling the run method of Zend_Application (which does the MVC stuff) and load only the resources that you need.

#!/usr/bin/php
<?php
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

set_include_path(realpath(APPLICATION_PATH . '/../library'));

require_once 'Zend/Application.php';

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
// Load only the ressources that you need
$application->getBootstrap()->bootstrap(
    array(
        'Db'
    )
);

// Do stuff

Take care of adapt this to the location of your cli script.

Maxence