MicroPHP里的MpInput类是专门用来获取各种输入数据,比如:GET,POST,SESSION,SERVER,COOKIE以及各种路由信息,同时获取GET,POST数据的方法具有验证功能。
使用input类获取数据有三个方式:
1.$this->input->xxx()
2.MpInput::xxx()
3.xxx()
提示:
1和2效果是一样的,$this->input其实就是MpInput的一个类实例对象。
3其实是2的一个快捷函数,参数和2一样。
xxx快捷函数列表如下,参数和对应的方法一致,具体参数可以参考下面的详细说明部分。
args() 对应:$this->input->parameters()
xss_clean() 对应:$this->input->xss_clean()
server() 对应:$this->input->server()
session() 对应:$this->input->session()
get() 对应:$this->input->get()
post() 对应:$this->input->post()
cookie() 对应:$this->input->cookie()
cookie_raw() 对应:$this->input->cookieRaw()
get_post() 对应:$this->input->get_post()
post_get() 对应:$this->input->post_get()
get_int() 对应:$this->input->get_int()
post_int() 对应:$this->input->post_int()
get_post_int() 对应:$this->input->get_post_int()
post_get_int() 对应:$this->input->post_get_int()
get_time() 对应:$this->input->get_time()
post_time() 对应:$this->input->post_time()
get_post_time() 对应:$this->input->get_post_time()
post_get_time() 对应:$this->input->post_get_time()
get_datetime() 对应:$this->input->get_datetime()
post_datetime() 对应:$this->input->post_datetime()
get_post_datetime() 对应:$this->input->get_post_datetime()
post_get_datetime() 对应:$this->input->post_get_datetime()
get_rule() 对应:$this->input->get_rule()
post_rule() 对应:$this->input->post_rule()
get_post_rule() 对应:$this->input->get_post_rule()
post_get_rule() 对应:$this->input->post_get_rule()
获取GET,POST数据有4个模式:
1.$this->input->get($key = null, $default = null, $xss_clean = false)
从GET里面获取数据。
2.$this->input->post($key = null, $default = null, $xss_clean = false)
从POST里面获取数据。
3.$this->input->get_post($key = null, $default = null, $xss_clean = false)
先从GET里面获取,没有的话再到POST里获取。
4.$this->input->post_get($key = null, $default = null, $xss_clean = false)
先从POST里面获取,没有的话再到GET里获取。
参数说明:
$key 键
$default 没有传递数据时使用的默认值
$xss_clean 是否对数据进行xss过滤,默认false不过滤
$this->input在4个模式下都有下面的xxx方法,这里只用get_xxx模式作为说明。
(1).$this->input->get_int($key, $min = null, $max = null, $default = null)
作用:获取一个整数。
参数说明:
$key 键
$min 最小值,为null不限制
$max 最大值,为null不限制
$default 默认值。格式错误或者不在范围,返回默认值
使用示例:
$this->input->get_int('id',1,null,1)
(2).$this->input->get_date($key, $min = null, $max = null, $default = null)
作用:获取日期,格式:2012-12-12
参数说明:
$key 键
$min 最小日期,格式:2012-12-12。为null不限制
$max 最大日期,格式:2012-12-12。为null不限制
$default 默认日期。格式错误或者不在范围,返回默认日期
使用示例:
$this->input->get_date('add_date',null,null,date('Y-m-d'))
(3).$this->input->get_time($key, $min = null, $max = null, $default = null)
作用:获取时间,格式:15:01:55
参数说明:
$key 键
$min 最小时间,格式:15:01:55。为null不限制
$max 最大时间,格式:15:01:55。为null不限制
$default 默认值。格式错误或者不在范围,返回默认值
使用示例:
$this->input->get_time('add_time',null,null,date('H:i:s'))
(4).$this->input->get_datetime($key, $min = null, $max = null, $default = null)
作用:获取日期时间,格式:2012-12-12 15:01:55
参数说明:
$key 键
$min 最小日期时间,格式:2012-12-12 15:01:55。为null不限制
$max 最大日期时间,格式:2012-12-12 15:01:55。为null不限制
$default 默认值。格式错误或者不在范围,返回默认值
使用示例:
$this->input->get_datetime('add_datetime',null,null,date('Y-m-d H:i:s'))
(5).$this->input->get_rule($rule, $key, $default = null)
作用:根据验证规则和键获取一个值
参数说明:
$rule 表单验证规则.示例:1.int 2. array('int','range[1,10]')
$key 键
$default 默认值。格式错误或者验证不通过,返回默认值。
使用示例:
$this->input->get_rule(MpRule::email(),'email')
验证规则很多,这么多规则怎么记忆?每次写验证都要看手册? 回答是No,只要在有自动提示的IDE里面$this->rule->xxx会自动提示xxx方法,xxx就是对应着所有的规则。
另外还也可以是MpRule::xxxx()也会自动提示。而且有良好的使用说明注释。
MpRule类封装了所有的规则生成的静态方法,方法名字和规则名称一致。
比如:
1.required规则,就有$this->rule->required()方法,也可以用MpRule::required()。
2.datetime规则,就有$this->rule->datetime($can_empty = false)方法,
也可以用MpRule::datetime($can_empty = false)。
获取COOKIE数据
$this->input->cookie($key = null, $default = null, $xss_clean = false)
参数说明:
$key 键
$default 没有传递数据时使用的默认值
$xss_clean 是否对数据进行xss过滤,默认false不过滤
获取SESSION数据
$this->input->session($key = null, $default = null)
参数说明:
$key 键
$default 没有数据时使用的默认值
获取SERVER数据
$this->input->server($key = null, $default = null)
参数说明:
$key 键,不区分大小写
$default 没有数据时使用的默认值
Ajax请求判断
$this->input->is_ajax(),如果是ajax请求返回true,不是就返回false。该方法之对jquery的ajax有效,该方法原理是通过判断jquery设置的头部变量HTTP_X_REQUESTED_WITH:XMLHttpRequest。
命令行执行判断
$this->input->isCli(),如果是在命令行下执行的返回ture,反之返回false。
xss过滤
$this->input->xss_clean($val),该方法会对$val进行xss过滤然后返回过滤后的数据。
获取路由信息相关的方法
(1).$this->input->module_name()
hmvc模块名称,没有模块就为空
(2).$this->input->method_path()
url中方法的路径,比如:1.home.index , 2.user.home.index ,user是文件夹
(3).$this->input->method_name()
url中方法名称,比如:index
(4).$this->input->method_prefix()
$system配置中方法前缀,比如:do
(5).$this->input->controller_path()
url中控制器的路径,比如:1.home 2.user.home ,user是文件夹
(6).$this->input->controller_name()
url中控制器名称,比如:home
(7).$this->input->folder_name()
url中文件夹名称,没有文件夹返回空
(8).$this->input->controller_file()
请求的控制器文件绝对路径,比如:/home/www/app/controllers/home.php
(9).$this->input->class_name()
请求的控制器类名称,比如:Home
(10).$this->input->class_method_name()
请求的控制器方法名称,比如:doIndex
(11).$this->input->parameters($key = null)
传递给控制器方法的所有参数的数组,参数为空时返回空数组
$key 参数的索引从0开始,如果传递了索引那么将返回索引对应的参数,不存在的索引将返回null
比如:
1.home.index/username/1234,那么返回的参数数组就是:array('username','1234')。
2.如果传递了$key,比如$key是1, 那么将返回1234。如果$key是2那么将返回null。
Powered By 狂奔的蜗牛 | 历史版本 | 源代码 | 快速入门 | MicroPHP@开源中国
源代码许可证 Apache License v2.0
MicroPHP官方QQ交流群 : 42807575 , 有问题欢迎加群交流