实际上,它比您想象的要容易得多。引导程序/应用程序组件和您现有的配置可以与CLI脚本一起重用,同时避免了MVC堆栈和HTTP请求中调用的不必要的重量。这是不使用wget的优点之一。
按照您的公共index.php启动脚本:
<?php// Define path to application directorydefined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));// Define application environmentdefined('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'));然后,您可以像在MVC应用程序中一样继续使用ZF资源:
$db = $application->getBootstrap()->getResource('db');$row = $db->fetchRow('SELECt * FROM something');如果您想在CLI脚本中添加可配置的参数,请查看Zend_Console_Getopt
如果发现自己有在MVC应用程序中也要调用的通用代码,请查看将其包装在一个对象中,然后从MVC和命令行应用程序中调用该对象的方法。这是一般的良好做法。



