开发者

How to create a script for the command line in PHP with Zend Framework?

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 elega开发者_如何学运维ntly as possible?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜