2009-07-22 134 views
1

如何使用Zend編寫示例控制檯應用程序?如何使用Zend編寫示例控制檯應用程序?

/Zend/Console/Getopt.php 

我只是想傳遞一個值作爲-v並將獲得版本信息。

輸入作爲

prjectfolder/console/version.php -v 

輸出:

Version 1.xxxxx 

在Zend公司與簡單的PHP與發送的lib包括方法如何我的代碼這一點。

回答

9

這是我如何處理應用程序的CLI接口的一個小例子。它包括我的Bootstrap和Zend Autoloader。一個更好的解決辦法是改變引導的CLI操作(不需要調度和這樣的東西),但我是一個懶惰的傢伙:-)

<?php 
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/')); 
define('APPLICATION_ENVIRONMENT', 'development'); 

/** 
* Setup for includes 
*/ 
set_include_path(
    APPLICATION_PATH . '/../library' . PATH_SEPARATOR . 
    APPLICATION_PATH . '/../application/models' . PATH_SEPARATOR . 
    APPLICATION_PATH . '/../application/extends'. PATH_SEPARATOR . 
    get_include_path()); 


/** 
* Zend Autoloader 
*/ 
require_once 'Zend/Loader/Autoloader.php'; 

$autoloader = Zend_Loader_Autoloader::getInstance(); 

/** 
* Register my Namespaces for the Autoloader 
*/ 
$autoloader->registerNamespace('My_'); 
$autoloader->registerNamespace('Db_'); 


/** 
* Include my complete Bootstrap 
* @todo change when time is left 
*/ 
require '../application/bootstrap.php'; 

/** 
* Setup the CLI Commands 
* ../application/cli.php --add 
* ../application/cli.php --scan 
* .. 
*/ 
try { 
    $opts = new Zend_Console_Getopt(
     array(
      'help'  => 'Displays usage information.', 
      'add'  => 'Add the Feeds to the Pipe', 
      'scan'  => 'Scan the Feeds in the Pipe', 
      'que'  => 'Process the Pipe', 
     ) 
    ); 

    $opts->parse(); 

} catch (Zend_Console_Getopt_Exception $e) { 
    exit($e->getMessage() ."\n\n". $e->getUsageMessage()); 
} 

if(isset($opts->help)) { 
    echo $opts->getUsageMessage(); 
    exit; 
} 

/** 
* Action : add 
*/ 
if(isset($opts->add)) { 
    // do something 
} 

/** 
* Action : scan 
*/ 
if(isset($opts->scan)) { 
    // do something 
} 

/** 
* Action : que 
*/ 
if(isset($opts->que)) { 
    // do something 
} 
-1

你可以在ZF docs找到所有你需要的細節。

+0

的是新Zend技術。所以不知道如何使用。安慰。 – coderex 2009-07-22 05:56:10

相關問題