0

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.