thinkphp6 输入变量过滤
2022-11-24 11:10:29
219
{{single.collect_count}}

thinkphp6 输入变量过滤

官方文档地址:

变量过滤

框架默认没有设置任何全局过滤规则,你可以在app\Request对象中设置filter全局过滤属性:

namespace app;

class Request extends \think\Request

{

protected $filter = ['htmlspecialchars'];

}

也支持使用Request对象进行全局变量的获取过滤,过滤方式包括函数、方法过滤,以及PHP内置的Types of filters,我们可以设置全局变量过滤方法,支持设置多个过滤方法,例如:

Request::filter(['strip_tags','htmlspecialchars']),

也可以在获取变量的时候添加过滤方法,例如:

Request::get('name','','htmlspecialchars'); // 获取get变量 并用htmlspecialchars函数过滤

Request::param('username','','strip_tags'); // 获取param变量 并用strip_tags函数过滤

Request::post('name','','org\Filter::safeHtml'); // 获取post变量 并用org\Filter类的safeHtml方法过滤

可以支持传入多个过滤规则,例如:

Request::param('username','','strip_tags,strtolower'); // 获取param变量 并依次调用strip_tags、strtolower函数过滤

如果当前不需要进行任何过滤的话,可以使用

// 获取get变量 并且不进行任何过滤 即使设置了全局过滤

Request::get('name', '', null);

对于body中提交的json对象,你无需使用php://input去获取,可以直接当做表单提交的数据使用,因为系统已经自动处理过了

支持 php 内置的字符串函数:

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}