获取请求参数


参数绑定

Document控制器show方法

  1. <?php
  2. namespace app\web\controller;
  3. use Timo\Core\Controller;
  4. Class Document extends Controller
  5. {
  6. public function show($id = 0)
  7. {
  8. echo $id;
  9. }
  10. }
  1. URL: http://www.timophp.com/document/show/100/

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

获取GET请求参数

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

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

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

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

  1. Request::getInt('type')

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

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

  1. Request::getString('name')

获取POST请求参数

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

例子:

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