获取请求参数


参数绑定

<?php
namespace app\web\controller;

use Timo\Core\Controller;

Class Document extends Controller
{
    public function show($id = 0)
    {
        echo $id;
    }
}
URL: http://www.timophp.com/document/show/100/

访问上面的URL,会打印出100,就相当于把url中show后面的100绑定到了show方法的$id参数上面,参数名称自定义

获取GET请求参数

1、get(参数名, [默认值], [过滤函数]);

例子:http://www.timophp.com/document/show/100/?type=1&from=index
$type = Request::get('type');
$type = Request::get('type', 0);
$type = Request::get('type', 0, 'intval');

2、getInt(参数名, [默认值]);

默认值可不传,默认为0,等价于Request::get(参数名, 0, 'interval')

Request::getInt('type')

3、getString(参数名, [默认值]);

默认值可不传,默认为空字符串,等价于Request::get(参数名, '', 'trim')

Request::getString('name')

获取POST请求参数

Request::post([参数名], [默认值], [过滤函数]);
Request::postInt(参数名, [默认值]);
Request::postString(参数名, [默认值]);

Request::post()获取整个post内容

例子:

<form action="http://www.timophp.com/document/show/100/" method="POST">
    <input type="text" name="email" />
    <input type="text" name="password" />
    <input type="submit" value="提交" />
</form>
$type = Request::post('email');
$type = Request::post('email', '');
$type = Request::post('password', '', 'trim');

获取json数据

Request::json([参数名], [默认值], [过滤函数]);
Request::jsonInt(参数名, [默认值]);
Request::jsonString(参数名, [默认值]);

Request::json()获取整个json内容