0

我有 Zend Framework 命令行应用程序。我在此应用程序中使用的所有模型和东西现在都在默认模块(应用程序前缀)中,但我想将它们移动到 cli 模块。当我将模型移动到 application/cli/model 文件夹并重命名类名时,自动加载器找不到它们。我也有管理模块,它工作正常。

这是我的 server.php 文件:

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(__DIR__ . '/../application'));
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
require_once 'Zend/Application.php';
$application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
);
$application->getBootstrap()->bootstrap(array('date', 'config'));

这是我的application.ini:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.modules[] = ""

我应该怎么做才能使模块在命令行模式下工作?

4

2 回答 2

2

我猜您缺少 Cli 模块的引导文件 - 它应该位于application/modules/cli/Bootstrap.php. 您可能还需要通过将模块和前端控制器资源添加到您的阵列来确保它们被初始化,从而为您提供:

$application->getBootstrap()->bootstrap(array('date', 'config', 'modules', 'frontController'));

如果不是,我们将需要更多信息,包括您遇到的错误、您尝试使用的类以及它的定义位置。

于 2013-02-04T09:19:47.633 回答
1

您需要的是一个 cli 入口点。

通常,您在浏览器中的 index.php(或 /)上输入应用程序,也就是通过 http。这通常会构建一个响应和一个路由器。因为你不是通过网络来的,所以你必须稍微调整一下。

因此,创建一个用作入口点的文件,例如 cli.php(从 public/index.php 复制它)。并对其进行一些调整

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

// bootstrap and retrieve the frontController resource
$front = $application->getBootstrap()
                     ->bootstrap('frontController')
                     ->getResource('frontController');

//Which part of the app we want to use?
$module     = 'default'; //or other module
$controller = '<your controller>';
$action     = '<your action>';

//create the request
$request = new Zend_Controller_Request_Simple ($action, $controller, $module, $options);

// set front controller options to make everything operational from CLI
$front->setRequest($request)
      ->setResponse(new Zend_Controller_Response_Cli())
      ->setRouter(new Custom_Controller_Router_Cli())
      ->throwExceptions(true);

// lets bootstrap our application and enjoy!
$application->bootstrap()
            ->run();
于 2013-02-04T07:49:58.357 回答