2009-07-22 9 views
1

Wie kann ich eine Beispielkonsole Anwendung mit Zend schreiben?Wie kann ich eine Beispiel-Konsolenanwendung mit Zend schreiben?

/Zend/Console/Getopt.php 

Ich möchte nur einen Wert als -v passieren und wird die Versionsinformationen erhalten.

Eingang als

prjectfolder/console/version.php -v 

Ausgang:

Version 1.xxxxx 

Wie ich dies in Zend mit einfachen PHP-Code kann mit Sende lib Methoden enthält.

Antwort

9

Dies ist ein kleines Beispiel, wie ich die CLI-Schnittstelle für eine Anwendung handhabe. Es beinhaltet meinen Bootstrap und den Zend Autoloader. Eine bessere Lösung ist es, die Bootstrap für CLI Operationen (keine Notwendigkeit für Dispatching und solche Sachen) zu ändern, aber ich bin ein fauler Kerl :-)

<?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

Sie können alle Details finden, die Sie in ZF docs benötigen.

+0

bin neu in Zend Technologie. also weiß nicht wie man es benutzt. Konsole. – coderex

Verwandte Themen