facebook twitter hatena line email

「Php/codeigniter/コマンド」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
(同じ利用者による、間の4版が非表示)
行1: 行1:
 
==コマンドでクラス実行==
 
==コマンドでクラス実行==
 +
===パターン1===
 
アプリケーションpathが、ci3appだったとき、Citestクラスのindexメソッドにアクセスする
 
アプリケーションpathが、ci3appだったとき、Citestクラスのindexメソッドにアクセスする
  env CI_ENV=development php ci3app/index.php citest index
+
  env CI_ENV=development php index.php citest index
 
<pre>
 
<pre>
 
class Citest extends MY_Controller {
 
class Citest extends MY_Controller {
行11: 行12:
 
</pre>
 
</pre>
  
 
+
===パターン2===
 
アプリケーションpathが、application_project_path1だったとき、Batchクラスのtestメソッドにアクセスする
 
アプリケーションpathが、application_project_path1だったとき、Batchクラスのtestメソッドにアクセスする
 
  CI_ENV=production php application_project_path1/index.php batch test
 
  CI_ENV=production php application_project_path1/index.php batch test
行25: 行26:
 
</pre>
 
</pre>
 
参考:https://qiita.com/horikeso/items/69b5329d87b30aa35d68
 
参考:https://qiita.com/horikeso/items/69b5329d87b30aa35d68
 +
 +
==パラメータ追加==
 +
アプリケーションpathが、ci3appだったとき、Citestクラスのindexメソッドにアクセスし、パラメータを設定
 +
env CI_ENV=development php index.php citest index --userId 123 --userAge 10
 +
 +
<pre>
 +
class Citest extends MY_Controller {
 +
    public function index()
 +
    {
 +
        echo "citest/index\n";
 +
        $params = $this->parseCliArguments();
 +
        log_message("info", "params=" . print_r($params,1));
 +
        echo 'userId: ' . $params['userId'] . ', userAge: ' . $params['userAge']. ', ';
 +
    }
 +
    /**
 +
    * CLI引数を解析する汎用メソッド
 +
    *
 +
    * @param array $args 引数配列(通常は $this->input->server('argv'))
 +
    * @param int $skip スキップする引数の数(CodeIgniterの場合は通常3)
 +
    * @return array 解析されたパラメータの連想配列
 +
    */
 +
    protected function parseCliArguments($args = null, $skip = 3)
 +
    {
 +
        // 引数が指定されていない場合はサーバーから取得
 +
        if ($args === null)
 +
        {
 +
            $args = $this->input->server('argv');
 +
        }
 +
        $params = [];
 +
        // 必要な引数だけを処理
 +
        for ($i = $skip; $i < count($args); $i++)
 +
        {
 +
            if (!isset($args[$i]))
 +
            {
 +
                continue;
 +
            }
 +
            // --で始まるオプションを解析
 +
            if (strpos($args[$i], '--') === 0)
 +
            {
 +
                $paramName = substr($args[$i], 2);
 +
                // 次の引数が値として使えるかチェック
 +
                if (isset($args[$i + 1]) && strpos($args[$i + 1], '--') !== 0)
 +
                {
 +
                    $params[$paramName] = $args[$i + 1];
 +
                    $i++; // 値をスキップ
 +
                }
 +
                else
 +
                {
 +
                    $params[$paramName] = true; // フラグオプション
 +
                }
 +
            }
 +
            // --で始まらない引数の処理(必要に応じて追加)
 +
            else
 +
            {
 +
                $params[] = $args[$i]; // 位置指定引数
 +
            }
 +
        }
 +
        return $params;
 +
    }
 +
}
 +
</pre>
 +
 +
==コマンドを有効にして、httpだけ403とする==
 +
<pre>
 +
class Nohttp extends CI_Controller {
 +
    public function __construct() {
 +
        parent::__construct();
 +
        // コマンドラインからの実行でなければ403エラー
 +
        if (!$this->input->is_cli_request()) {
 +
            show_error('HTTPアクセスは許可されていません', 403);
 +
        }
 +
    }
 +
    public function index()
 +
    {
 +
        $data = [
 +
            'title' => 'hello!',
 +
            'message' => 'helloworld!!',
 +
        ];
 +
        $this->load->view('hello', $data);
 +
    }
 +
}
 +
</pre>
 +
 +
ttp://localhost/nohttp/index は403
 +
 +
env CI_ENV=development php index.php nohttp index # これは通る

2025年6月17日 (火) 12:43時点における最新版

コマンドでクラス実行

パターン1

アプリケーションpathが、ci3appだったとき、Citestクラスのindexメソッドにアクセスする

env CI_ENV=development php index.php citest index
class Citest extends MY_Controller {
    public function index()
    {
        echo 'citest/index';
    }
}

パターン2

アプリケーションpathが、application_project_path1だったとき、Batchクラスのtestメソッドにアクセスする

CI_ENV=production php application_project_path1/index.php batch test
class Batch extends MY_Controller
{
    public function test()
    {
        echo 'batch/test';
    }
}

参考:https://qiita.com/horikeso/items/69b5329d87b30aa35d68

パラメータ追加

アプリケーションpathが、ci3appだったとき、Citestクラスのindexメソッドにアクセスし、パラメータを設定

env CI_ENV=development php index.php citest index --userId 123 --userAge 10
class Citest extends MY_Controller {
    public function index()
    {
        echo "citest/index\n";
        $params = $this->parseCliArguments();
        log_message("info", "params=" . print_r($params,1));
        echo 'userId: ' . $params['userId'] . ', userAge: ' . $params['userAge']. ', ';
    }
    /**
     * CLI引数を解析する汎用メソッド
     * 
     * @param array $args 引数配列(通常は $this->input->server('argv'))
     * @param int $skip スキップする引数の数(CodeIgniterの場合は通常3)
     * @return array 解析されたパラメータの連想配列
     */
    protected function parseCliArguments($args = null, $skip = 3)
    {
        // 引数が指定されていない場合はサーバーから取得
        if ($args === null)
        {
            $args = $this->input->server('argv');
        }
        $params = [];
        // 必要な引数だけを処理
        for ($i = $skip; $i < count($args); $i++)
        {
            if (!isset($args[$i]))
            {
                continue;
            }
            // --で始まるオプションを解析
            if (strpos($args[$i], '--') === 0)
            {
                $paramName = substr($args[$i], 2);
                // 次の引数が値として使えるかチェック
                if (isset($args[$i + 1]) && strpos($args[$i + 1], '--') !== 0)
                {
                    $params[$paramName] = $args[$i + 1];
                    $i++; // 値をスキップ
                }
                else
                {
                    $params[$paramName] = true; // フラグオプション
                }
            }
            // --で始まらない引数の処理(必要に応じて追加)
            else
            {
                $params[] = $args[$i]; // 位置指定引数
            }
        }
        return $params;
    }
}

コマンドを有効にして、httpだけ403とする

class Nohttp extends CI_Controller {
    public function __construct() {
        parent::__construct();
        // コマンドラインからの実行でなければ403エラー
        if (!$this->input->is_cli_request()) {
            show_error('HTTPアクセスは許可されていません', 403);
        }
    }
    public function index()
    {
        $data = [
            'title' => 'hello!',
            'message' => 'helloworld!!',
        ];
        $this->load->view('hello', $data);
    }
}

ttp://localhost/nohttp/index は403

env CI_ENV=development php index.php nohttp index # これは通る