http://www.21andy.com/blog/20100211/1670.html
下载 MY_URI.php 复制到 application/libraries/ 目录
下载: MY_URI.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* MY_URI Class
*
* Parses URIs and determines routing
*
* @package CodeIgniter
* @subpackage Libraries
* @category URI
* @author ExpressionEngine Dev Team
* @modified Philip Sturgeon
* @link http://codeigniter.com/user_guide/libraries/uri.html
*/
class MY_URI extends CI_URI {
/**
* Get the URI String, with added support for command line
*
* @access private
* @return string
*/
function _fetch_uri_string()
{
if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
{
// If the URL has a question mark then it's simplest to just
// build the URI string from the zero index of the $_GET array.
// This avoids having to deal with $_SERVER variables, which
// can be unreliable in some environments
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
$this->uri_string = key($_GET);
return;
}
// Is there a PATH_INFO variable?
// Note: some servers seem to have trouble with getenv() so we'll test it two ways
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO']: @getenv('PATH_INFO');
if (trim($path, '/') != '' && $path != "/".SELF)
{
$this->uri_string = $path;
return;
}
// No PATH_INFO?... What about QUERY_STRING?
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
if (trim($path, '/') != '')
{
$this->uri_string = $path;
return;
}
// No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
$path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
if (trim($path, '/') != '' && $path != "/".SELF)
{
// remove path and script information so we have good URI data
$this->uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $path);
return;
}
// Has arguments and no server name, must be command line
if(isset($_SERVER['argv']) && !isset($_SERVER['SERVER_NAME']))
{
$this->uri_string = $this->_parse_cli_args();
return;
}
// We've exhausted all our options...
$this->uri_string = '';
}
elseif(strtoupper($this->config->item('uri_protocol')) == 'CLI')
{
$this->uri_string = $this->_parse_cli_args();
}
else
{
$uri = strtoupper($this->config->item('uri_protocol'));
if ($uri == 'REQUEST_URI')
{
$this->uri_string = $this->_parse_request_uri();
return;
}
$this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
}
// If the URI contains only a slash we'll kill it
if ($this->uri_string == '/')
{
$this->uri_string = '';
}
}
// Convert arguments into a
function _parse_cli_args() {
// Get all arguments from command line
$args = $_SERVER['argv'];
// Remove the first, its the file name
unset($args[0]);
return '/'.implode('/', $args);
}
}
// END MY_URI Class
/* End of file URI.php */
/* Location: ./application/libraries/MY_URI.php */
?>
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* MY_URI Class
*
* Parses URIs and determines routing
*
* @package CodeIgniter
* @subpackage Libraries
* @category URI
* @author ExpressionEngine Dev Team
* @modified Philip Sturgeon
* @link http://codeigniter.com/user_guide/libraries/uri.html
*/
class MY_URI extends CI_URI {
/**
* Get the URI String, with added support for command line
*
* @access private
* @return string
*/
function _fetch_uri_string()
{
if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
{
// If the URL has a question mark then it's simplest to just
// build the URI string from the zero index of the $_GET array.
// This avoids having to deal with $_SERVER variables, which
// can be unreliable in some environments
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
$this->uri_string = key($_GET);
return;
}
// Is there a PATH_INFO variable?
// Note: some servers seem to have trouble with getenv() so we'll test it two ways
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO']: @getenv('PATH_INFO');
if (trim($path, '/') != '' && $path != "/".SELF)
{
$this->uri_string = $path;
return;
}
// No PATH_INFO?... What about QUERY_STRING?
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
if (trim($path, '/') != '')
{
$this->uri_string = $path;
return;
}
// No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
$path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
if (trim($path, '/') != '' && $path != "/".SELF)
{
// remove path and script information so we have good URI data
$this->uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $path);
return;
}
// Has arguments and no server name, must be command line
if(isset($_SERVER['argv']) && !isset($_SERVER['SERVER_NAME']))
{
$this->uri_string = $this->_parse_cli_args();
return;
}
// We've exhausted all our options...
$this->uri_string = '';
}
elseif(strtoupper($this->config->item('uri_protocol')) == 'CLI')
{
$this->uri_string = $this->_parse_cli_args();
}
else
{
$uri = strtoupper($this->config->item('uri_protocol'));
if ($uri == 'REQUEST_URI')
{
$this->uri_string = $this->_parse_request_uri();
return;
}
$this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
}
// If the URI contains only a slash we'll kill it
if ($this->uri_string == '/')
{
$this->uri_string = '';
}
}
// Convert arguments into a
function _parse_cli_args() {
// Get all arguments from command line
$args = $_SERVER['argv'];
// Remove the first, its the file name
unset($args[0]);
return '/'.implode('/', $args);
}
}
// END MY_URI Class
/* End of file URI.php */
/* Location: ./application/libraries/MY_URI.php */
?>
然后设置config
$config['uri_protocol'] = "AUTO"; // Works for web and command line
$config['uri_protocol'] = "CLI"; // Command line only
$config['uri_protocol'] = isset($_SERVER['REQUEST_URI']) ? 'PATH_INFO' : 'CLI'; // working on web with a specific uri type and command line at the same time, change path info to any of the normal CI uri types.
$config['uri_protocol'] = "CLI"; // Command line only
$config['uri_protocol'] = isset($_SERVER['REQUEST_URI']) ? 'PATH_INFO' : 'CLI'; // working on web with a specific uri type and command line at the same time, change path info to any of the normal CI uri types.
接着就可以像这样使用了
php index.php controller method param1 etc
下面是使命令行执行 CodeIgniter 有更好的交互的扩展,具体方法看源代码就明白了
For easier interaction with command line input/output/prompts etc try this CLI library
CodeIgniter-CLI
===============CodeIgniter-CLI is a CodeIgniter library which alllows you to work with CodeIgniter over
the command line. Beofre this can be enabled you need follow the instructions below.http://codeigniter.com/wiki/CI_on_the_command_line/
Extra
-----If you'd like to request changes, report bug fixes, or contact
the developer of this library, email
复制 CLI.php 到 application/libraries/ 目录
下载: CLI.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @author Philip Sturgeon
* @created 11 Dec 2008
*/
class CLI {
private $CI; // CodeIgniter instance
var $wait_msg = 'Press any key to continue...';
function CLI() {
$this->CI =& get_instance();
log_message('debug', 'CLI Class Initialized');
}
// Output a line (or lines) to the command line
function write($output = '') {
// If there are multiple lines, seperate them by newlines
if(is_array($output)) {
$output = implode("\n", $output);
}
// Output the lot
fwrite(STDOUT, $output."\n");
return $this;
}
// Read in a variable from the command line
function read() {
// Work out whats what based on what params are given
$args = func_get_args();
// Ask question with options
if(count($args) == 2) {
list($output, $options)=$args;
// No question (probably been asked already) so just show options
} elseif(count($args) == 1 && is_array($args[0])) {
$output = '';
$options = $args[0];
// Question without options
} elseif(count($args) == 1 && is_string($args[0])) {
$output = $args[0];
$options = array();
// Run out of ideas, EPIC FAIL!
} else {
$output = '';
$options = array();
}
// If a question has been asked with the read
if(!empty($output)) {
$options_output = '';
if(!empty($options)) {
$options_output = ' [ '.implode(', ', $options).' ]';
}
fwrite(STDOUT, $output.$options_output.': ');
}
// Read the input from keyboard.
$input = trim(fgets(STDIN));
// If options are provided and the choice is not in the array, tell them to try again
if(!empty($options) && !in_array($input, $options)) {
$this->write('This is not a valid option. Please try again.');
$this->new_line();
$input = $this->read($output, $options);
}
// Read the input
return $input;
}
function new_line($lines = 1) {
// Do it once or more, write with empty string gives us a new line
for($i = 0; $i < $lines; $i++) $this->write();
return $this;
}
function clear_screen()
{
# xterm / konsole: array_map(create_function('$a', 'print chr($a);'), array(27, 91, 72, 27, 91, 50, 74));
# bash: passthru('clear');
/* unix: if ($proc = popen("(cls)2>&1","r")){
$result = '';
while (!feof($proc)) $result .= fgets($proc, 1000);
pclose($proc);
return $result;
} */
// works, ugly
$this->new_line(100);
/* $clearscreen = chr(27)."[H".chr(27)."[2J";
print $clearscreen; */
/*echo "Starting Iteration" . "\n\r";
for ($i=0;$i<10000;$i++) {
echo "\r";
}
echo "Ending Iteration" . "\n\r";*/
}
// Thanks to Daniel Morris (danielxmorris.com)
function beep($beeps = 1) {
$string_beeps = '';
// Output the correct number of beep characters
for ($i = 0; $i < $beeps; $i++) $string_beeps .= "\x07";
print $string_beeps;
return $this;
}
function wait($seconds = 0, $countdown = FALSE) {
// Diplay the countdown
if($countdown == TRUE) {
$i = $seconds;
while ( $i > 0 ) {
fwrite(STDOUT, "\r".$i.'...');
sleep(1);
$i--;
}
// No countdown timer please
} else {
// Set number of seconds?
if($seconds > 0) {
sleep($seconds);
// No seconds mentioned, lets wait for user input
} else {
$this->write($this->wait_msg);
$this->read();
}
}
return $this;
}
}
// END CLI Class
/* End of file CLI.php */
/* Location: ./application/libraries/CLI.php */
?>
/**
* @author Philip Sturgeon
* @created 11 Dec 2008
*/
class CLI {
private $CI; // CodeIgniter instance
var $wait_msg = 'Press any key to continue...';
function CLI() {
$this->CI =& get_instance();
log_message('debug', 'CLI Class Initialized');
}
// Output a line (or lines) to the command line
function write($output = '') {
// If there are multiple lines, seperate them by newlines
if(is_array($output)) {
$output = implode("\n", $output);
}
// Output the lot
fwrite(STDOUT, $output."\n");
return $this;
}
// Read in a variable from the command line
function read() {
// Work out whats what based on what params are given
$args = func_get_args();
// Ask question with options
if(count($args) == 2) {
list($output, $options)=$args;
// No question (probably been asked already) so just show options
} elseif(count($args) == 1 && is_array($args[0])) {
$output = '';
$options = $args[0];
// Question without options
} elseif(count($args) == 1 && is_string($args[0])) {
$output = $args[0];
$options = array();
// Run out of ideas, EPIC FAIL!
} else {
$output = '';
$options = array();
}
// If a question has been asked with the read
if(!empty($output)) {
$options_output = '';
if(!empty($options)) {
$options_output = ' [ '.implode(', ', $options).' ]';
}
fwrite(STDOUT, $output.$options_output.': ');
}
// Read the input from keyboard.
$input = trim(fgets(STDIN));
// If options are provided and the choice is not in the array, tell them to try again
if(!empty($options) && !in_array($input, $options)) {
$this->write('This is not a valid option. Please try again.');
$this->new_line();
$input = $this->read($output, $options);
}
// Read the input
return $input;
}
function new_line($lines = 1) {
// Do it once or more, write with empty string gives us a new line
for($i = 0; $i < $lines; $i++) $this->write();
return $this;
}
function clear_screen()
{
# xterm / konsole: array_map(create_function('$a', 'print chr($a);'), array(27, 91, 72, 27, 91, 50, 74));
# bash: passthru('clear');
/* unix: if ($proc = popen("(cls)2>&1","r")){
$result = '';
while (!feof($proc)) $result .= fgets($proc, 1000);
pclose($proc);
return $result;
} */
// works, ugly
$this->new_line(100);
/* $clearscreen = chr(27)."[H".chr(27)."[2J";
print $clearscreen; */
/*echo "Starting Iteration" . "\n\r";
for ($i=0;$i<10000;$i++) {
echo "\r";
}
echo "Ending Iteration" . "\n\r";*/
}
// Thanks to Daniel Morris (danielxmorris.com)
function beep($beeps = 1) {
$string_beeps = '';
// Output the correct number of beep characters
for ($i = 0; $i < $beeps; $i++) $string_beeps .= "\x07";
print $string_beeps;
return $this;
}
function wait($seconds = 0, $countdown = FALSE) {
// Diplay the countdown
if($countdown == TRUE) {
$i = $seconds;
while ( $i > 0 ) {
fwrite(STDOUT, "\r".$i.'...');
sleep(1);
$i--;
}
// No countdown timer please
} else {
// Set number of seconds?
if($seconds > 0) {
sleep($seconds);
// No seconds mentioned, lets wait for user input
} else {
$this->write($this->wait_msg);
$this->read();
}
}
return $this;
}
}
// END CLI Class
/* End of file CLI.php */
/* Location: ./application/libraries/CLI.php */
?>