如果我理解正确,您正在尝试从 CLI/CMD 运行您的应用程序,方法是调用 basecmd.php,它将设置变量/常量以使 index.php 正常工作
您的 basecmd.php 应如下所示:
#!/usr/bin/env php
<?php
// basecmd.php
require_once 'path/to/Zend/Console/Getopt.php';
try {
$opts = new Zend_Console_Getopt(
array(
'app-env|e=s' => 'Application environment',
'app-path|ap=s' => 'Path to application folder',
'lib-path|lp=s' => 'Path to library',
// more options
)
);
$opts->parse();
if (!($path = $opts->getOption('ap'))) { // cli param is missing
throw new Exception("You must specify application path");
}
define('APPLICATION_PATH', $path);
// process other params and setup more constants/variables
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
} catch (Exception $e) {
echo $e->getMessage() . "\n";
exit;
}
// it is wise to setup another constant so application can determine is it a web or cli call
define('RUN_VIA', 'cli');
// if all done correctly include application loader script
include 'index.php';
在您的 index.php 中,您应该只测试是否已经定义了常量或变量:
<?php
// index.php
defined('APPLICATION_PATH') // is it defined
or define('APPLICATION_PATH', '../application'); // no? then define it
defined('RUN_VIA')
or define('RUN_VIA', 'web');
// ... rest of the code
我希望这可以帮助您走上正轨;)