路老闆個人品牌網站
0
  • 會員登入
  • 關於我們
    回主選單
    • 關於我們
  • 服務項目
  • 我的錄音小間
  • 課程活動
    回主選單
    • 線上課程
    • 預約服務
    • 活動報名
  • 商品總覽
  • 創作與紀錄
    回主選單
    • 部落格
    • 影像紀錄
    • Podcast
    • 我的作品
  • 最新消息
  • 聯絡我們
  • Join us
  • Login
  • 0
    路老闆個人品牌網站
  • 關於我們
    關於我們
  • 服務項目
  • 我的錄音小間
  • 課程活動
    線上課程 預約服務 活動報名
  • 商品總覽
  • 創作與紀錄
    部落格 影像紀錄 Podcast 我的作品
  • 最新消息
  • 聯絡我們
部落格
  • 文章總覽
  • 分類
  • 技術筆記
  • 日本旅遊
  • 網站搬家
  • 時事觀察
  • 投資理財
  • 公司經營
  • 主機架設
  • 接案
  • 遊戲心得
  • 產品開箱
  • 網頁相關
  • 金流串接
  • 遊戲經濟
  • 閒聊
  • 直銷
  • CodeIgniter
  • cpanel
  • 出書材料
  • 個人品牌
  • SEO
  • 讀書心得
  • 金流物流
  • 線上課程心得
  • 日劇
    • 大河劇
      • 真田丸
  • AI
91app ClubHouse php8 imunify360 cdn apache php nginx linode digitalocean
  1. 首頁
  2. 部落格
  3. CodeIgniter CLI (command line mode) 修改

CodeIgniter CLI (command line mode) 修改

2010 Dec 26 未分類

有些時候可能會使用 命令列的方式去 執行一些 cron 的 工作
用 web 模式可能會 timeout, 把 execute time 設定成很長 也不是辦法

所以要把 CodeIgniter 改一下
以便在 CodeIgniter 的 架構下 跑 CLI


首先下載 MY_URI.php

如果連結壞了 可以抓底下的 code


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 */



然後把檔案貼到 application/libraries/ 裡面

然後 到 config.php 加入以下內容
$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.

就設定完成了

命令列的執行方法如下
php index.php controller method param1 etc
  • 分享此文章
0則留言

相關文章

最近好軟

保險真的保本/保值嘛?

新網紅讀書會 X #阿薩布魯聊書會

在Ubuntu中安裝VMware Workstation

又一家爛主機 燃燒G

[旅遊] JR PASS 購買/使用指南

關於路老闆

推薦連結

  • Alice 你的理財陪跑教練
  • 羅鈞鴻/小虎老師
  • 徐慧玲Lynn
  • 寶寶牙醫 沈醫師
  • 保險資訊懶人包
  • 你的好感度教練
  • 天下為暢

聯絡我們

  • Email: louis@boss-louis.tw
  • 營業時間: 09:00~18:00
  • 聯絡電話: 02-77252950
  • 地址: 106 台北市大安區忠孝東路四段270號17樓
  • 隱私權政策
COPYRIGHT ©路老闆個人品牌網站 All rights reserved | Powered by 路老闆