optima1 answer started me on the right track, but I would like to share some additional steps that I need to help the url() and serverUrl() CLI serverUrl() script.
Medium detection
The first task is to determine the server environment. This often happens in a .htaccess variable or FastCGI environment variable, so it does not load into the CLI. I am using the INI file to match the server name in the environment.
[ServerEnvironments] PROD-SERVER = "production" STAGE-SERVER = "staging" MY-LOCAL-VM = "development"
Then my CLI script sets the APPLICATION_ENV constant accordingly.
$iniConfig = parse_ini_file(rtrim(dirname(__FILE__), '\/') . '/servers.ini', TRUE); $environment = isset($iniConfig['ServerEnvironments'][getenv('COMPUTERNAME')]) ? $iniConfig['ServerEnvironments'][getenv('COMPUTERNAME')] : 'production'; defined('APPLICATION_ENV') || define('APPLICATION_ENV', $environment);
Superglobal Server Population
The rest of the script continues normally, as shown in David's example . However, I load all resources (empty call to bootstrap() ). I also add an additional INI file when creating an instance of Zend_Application , for example:
$application = new Zend_Application( APPLICATION_ENV, array('config' => array(APPLICATION_PATH . '/configs/application.ini', APPLICATION_PATH . '/configs/cli.ini')));
The INI CLI file will store the request information, which is usually provided by the web server.
[production] cli.request.serverVars.HTTPS = "on" cli.request.serverVars.HTTP_SCHEME = "https" ; Host name, including port if non-standard. cli.request.serverVars.HTTP_HOST = "example.com" ; Absolute path to the web root. Exclude trailing slash. cli.request.serverUrl = "https://example.com" ; Relative path from web root to the Zend Framework application. Include leading slash. cli.request.baseUrl = "/ZF" [staging : production] cli.request.serverUrl = "https://stage.example.com" cli.request.serverVars.HTTP_HOST = "stage.example.com" [development : production] cli.request.serverUrl = "https://dev.example.com:4433" cli.request.serverVars.HTTP_HOST = "dev.example.com:4433"
After calling bootstrap() CLI script populates the $_SERVER module. Note that during bootstrap, I save the application configuration to Zend_Registry .
$config = Zend_Registry::get('config'); foreach($config->cli->request->serverVars as $name => $value) { $_SERVER[$name] = $value; }
Build URIs and Submit
The last hurdle is that I use custom routes instead of /module/controller/action , and the latter format is disabled ( Zend_Controller_Router_Rewrite::removeDefaultRoutes() ). Thus, the CLI script should indicate the route, not the module, controller, and action.
$route = 'My/Custom/Route'; $spoofedUri = $config->cli->request->serverUrl . rtrim($config->cli->request->baseUrl, '/') . '/' . $route; $request = new Zend_Controller_Request_Http($spoofedUri); $request->setBaseUrl($config->cli->request->baseUrl); $front = Zend_Controller_Front::getInstance(); $front->setRequest($request) ->setResponse(new Zend_Controller_Response_Cli()) ->dispatch();
This may not solve every mismatch between the web and CLI requests (you may need to fill out more $_SERVER ), but url() and serverUrl() were set for me.