CLIRequest クラス
コマンドラインからの呼び出しによるリクエストの場合、リクエストオブジェクトは実際にはCLIRequest
です。従来のリクエストと同様に動作しますが、便宜のためにいくつかのアクセサーメソッドが追加されています。
追加のアクセサー
getSegments()
パスの一部と見なされるコマンドライン引数の配列を返します。
<?php
// command line: php index.php users 21 profile --foo bar
echo $request->getSegments(); // ['users', '21', 'profile']
getPath()
再構築されたパスを文字列として返します。
<?php
// command line: php index.php users 21 profile --foo bar
echo $request->getPath(); // users/21/profile
getOptions()
オプションと見なされるコマンドライン引数の配列を返します。
<?php
// command line: php index.php users 21 profile --foo bar
echo $request->getOptions(); // ['foo' => 'bar']
getOption($which)
オプションと見なされる特定のコマンドライン引数の値を返します。
<?php
// command line: php index.php users 21 profile --foo bar
echo $request->getOption('foo'); // bar
echo $request->getOption('notthere'); // null
getOptionString()
オプションのコマンドライン文字列を再構築して返します。
<?php
// command line: php index.php users 21 profile --foo bar
echo $request->getOptionString(); // -foo bar
最初の引数にtrue
を渡すと、長いオプションを2つのダッシュを使用して記述しようとします。
<?php
// php index.php user 21 --foo bar -f
echo $request->getOptionString(); // -foo bar -f
echo $request->getOptionString(true); // --foo bar -f