如何运行从CLI使用ZendFramework库的脚本

时间:2013-11-26 20:32:08

标签: php zend-framework command-line-interface

我打算使用CLI中的ZendFramework库运行脚本。 我的脚本如下:

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Rest_Client');

它可以在浏览器中运行,但在命令提示符下失败。如何让脚本从CLI运行?

谢谢!

1 个答案:

答案 0 :(得分:1)

这篇文章有一些您正在寻找的信息:Running a Zend Framework action from command line


下面你会找到我在我的应用程序中编写并用于cron作业的完整功能代码......

需要执行以下操作:

  1. 在您的cli文件中提取所需文件
  2. 初始化应用程序和引导资源。在这里,您可以捕获cli参数和设置请求对象,以便正确分派它。
  3. 设置控制器目录
  4. 运行应用程序
  5. 以下是有关Zend_Console_Getopt的文档,可帮助您了解如何使用cli params。

    cli.php

    的代码
    <?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', 'development');
    
    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
        realpath(APPLICATION_PATH . '/libraries'),
        get_include_path(),
    )));
    
    //  initialize application
    require_once 'My/Application/Cron.php';
    $application = new My_Application_Cron(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
    $application->bootstrap();
    
    //
    Zend_Controller_Front::getInstance()->setControllerDirectory(APPLICATION_PATH . '/../scripts/controllers');
    
    //
    $application->run();
    

    My_Application_Cron班级代码:

    <?php
    
    //  because we are extending core application file we have to explicitly require it
    //  the autoloading occurs in the bootstrap which is part of this class
    require_once 'Zend/Application.php';
    
    class My_Application_Cron extends Zend_Application
    {
    
        protected $_cliRequest = null;
    
        protected function _bootstrapCliRequest()
        {
            $this->_isCliBootstapped = true;
    
            try {
                $opts = array(
                    'help|h' => 'Displays usage information.',
                    'action|a=s' => 'Action to perform in format of module.controller.action',
                    'params|p=p' => 'URL-encoded parameter list',
                    //'verbose|v' => 'Verbose messages will be dumped to the default output.',
                );
                $opts = new Zend_Console_Getopt($opts);
                $opts->setOption('ignoreCase', true)
                    ->parse();
    
            } catch (Zend_Console_Getopt_Exception $e) {
                exit($e->getMessage() . "\n\n" . $e->getUsageMessage());
            }
    
            //  See if help needed
            if (isset($opts->h)) {
                $prog = $_SERVER['argv'][0];
                $msg = PHP_EOL
                    . $opts->getUsageMessage()
                    . PHP_EOL . PHP_EOL
                    . 'Examples:' . PHP_EOL
                    . "php $prog -a index.index'" . PHP_EOL
                    . "php $prog -a index.index -p 'fname=John&lname=Smith'" . PHP_EOL
                    . "php $prog -a index.index -p 'name=John+Smith'" . PHP_EOL
                    . "php $prog -a index.index -p 'name=John%20Smith'" . PHP_EOL
                    . PHP_EOL;
    
                echo $msg;
                exit;
            }
    
            //  See if controller/action are set
            if (isset($opts->a)) {
                //  Prepare necessary variables
                $params = array();
                $reqRoute = array_reverse(explode('.', $opts->a));
                @list($action, $controller, $module) = $reqRoute;
    
                //  check if request parameters were sent
                if ($opts->p) {
                    parse_str($opts->p, $params);
                }
    
                //
                $this->_cliRequest = new Zend_Controller_Request_Simple($action, $controller, $module);
                $this->_cliRequest->setParams($params);
            }
        }
    
        public function bootstrap($resource = null)
        {
            $this->_bootstrapCliRequest();
    
            return parent::bootstrap($resource);
        }
    
        public function run()
        {
            //  make sure bootstrapCliRequest was executed prior to running the application
            if (!($this->_cliRequest instanceof Zend_Controller_Request_Simple)) {
                throw new Exception('application required "bootstrapCliRequest"');
            }
    
            //  set front controller to support CLI
            $this->getBootstrap()->getResource('frontcontroller')
                ->setRequest($this->_cliRequest)
                ->setResponse(new Zend_Controller_Response_Cli())
                ->setRouter(new Custom_Controller_Router_Cli())
                ->throwExceptions(true);
    
            //  run the application
            parent::run();
        }
    
    }
    

    要了解如何运行cli scipt,只需在终端输入以下命令:

    #> php /path/to/cli.php -h
    

    希望这会对你和其他人有所帮助!