sivka / request
v1.0.2
2019-03-24 13:16 UTC
This package is auto-updated.
Last update: 2024-09-25 08:03:58 UTC
README
此库提供了访问请求数据的简单方式。
安装
composer require sivka/request
目录
示例
// $_POST = ['id' => 1, 'name' => 'Valera'] use Sivka\Request; echo Request::post('name'); // Valera $post = Request::post(); echo $post->name; // Valera echo $post->not_defined_var; // NULL
可用方法
Request::get(); // $_GET Request::post(); // $_POST Request::files(); // $_FILES Request::session(); // $_SESSION Request::cookie(); // $_COOKIE Request::server(); // $_SERVER Request::headers(); // http headers
所有方法具有相同的签名。每个方法都返回请求对象。如果方法带有参数调用,它返回指定键的值,如果键不存在则返回 NULL。
请求对象方法
set
为指定键设置值
$post = Request::post(); $post->set('id', 2); // or directly $post->id = 2; // array maybe used $newData = ['surname' => 'Smith', 'age' => 33]; $post->set($newData);
int
返回转换为整数或 0 的值
echo $post->int('id'); // 2
string
返回转换为字符串或空字符串的值
echo $post->string('id'); // '2'
get
如果存在则返回值,否则返回 null。
echo $post->get('id'); // 2 // or simply echo $post->id; // 2
方法 int
、string
和 get
允许指定默认值作为第二个可选参数,如果键不存在于请求对象中
echo $post->get('notDefined', 'define_me'); // define_me
count
返回值的数量
$post->count();
toJson
返回请求对象的 json 表示形式
$post->toJson();
toArray
将请求对象作为数组返回
$post->toArray();
delete
从请求对象中删除键
$post->delete('id');
has
检查键是否存在于请求对象中
echo $post->has('name'); // true
keys
返回请求对象中的键数组
$post->keys();
这样做有什么好处?无需使用此类构造
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
许可证
此项目采用MIT 许可证