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($key)

返回被视为选项的特定命令行参数的值:

<?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 传递给第一个参数会尝试使用两个连字符来写长选项:

<?php

// php index.php user 21 --foo bar -f
echo $request->getOptionString();     // -foo bar -f
echo $request->getOptionString(true); // --foo bar -f