Phalcon搭建多模块框架七:封装和过滤post、request、json(input流获取)参数

本文介绍了在Phalcon框架中如何封装和过滤POST、REQUEST及JSON输入流的参数。通过修改BaseController,实现类似GET请求的便捷过滤机制。在FilterController中进行测试,分别展示了对POST、REQUEST及JSON数据的过滤效果。
摘要由CSDN通过智能技术生成

在phalcon在控制器中获取以下数据:

post数据是通过$this->request->getPost()来获取的,查看源码可以看到获取单个值时如果设置了过滤参数就会进行过滤,但是获取全部值时(即不传第一个参数$name)是不进行过滤,直接返回的($_POST)。

request数据是通过$this->request->get()来获取的,过滤机制同上面一样。

json数据是通过$this->request->getJsonRawBody()来获取的,通过传参(true or false)来返回数组 or 对象。而$this->request->getJsonRawBody()其实是通过request->getRawBody()来获取数据并用json_decode解码的。而request->getRawBody()是通过file_get_contents("php://input")来获取数据的。这期间也没有过滤。

所以我们可以类似get()那样封装一下,使我们更便捷的获取参数。
这里写图片描述

打开common/BaseController.php

<?php
/** 
 * @desc 控制器基类
 * @author zhaoyang 
 * @date 2018年5月8日 下午10:37:37 
 */
namespace Common;

use Phalcon\Mvc\Controller;

class BaseController extends Controller {
   

    /** 
     * @desc 获取get参数
     * @param string $name 参数名
     * @param string|array $filter 过滤类型,支持string、trim、absint、int、email、float、int!、float!、alphanum、striptags、lower、upper、url、special_chars
     * 当为false时,不使用默认过滤,当为字符串例如'string,trim'时采用参数过滤 ,当为数组例如['string','trim']时采用参数+默认过滤,当为null等其他值时时采用默认过滤
     * @param mixed $defaultValue 默认值
     * @param bool $noRecursive 不递归过滤(即$data不作为数组循环过滤)
     * @return mixed
     * @author zhaoyang 
     * @date 2018年5月8日 下午10:38:50 
     */
    final protected function get(string $name = null, $filter = null, $defaultValue = null, bool $noRecursive = false) {
   
        $data = array_merge($this->request->getQuery(), $this->dispatcher->getParams());
        unset($data['_url']);
        return $this->sanitize($data, $name, $filter, $defaultValue, $noRecursive);
    }

    /** 
     * @desc 获取post参数
     * @param string $name 参数名
     * @param string|array $filter 过滤类型,支持string、trim、absint、int、email、float、int!、float!、alphanum、striptags、lower、upper、url、special_chars
     * 当为false时,不使用默认过滤,当为字符串'string,trim'时采用参数过滤 ,当为数组['string','trim']时采用参数+默认过滤,当为null等其他值时时采用默认过滤
     * @param mixed $defaultValue 默认值
     * @param bool $noRecursive 不递归过滤(即$data不作为数组循环过滤)
     * @return mixed
     * @author zhaoyang 
     * @date 2018年5月9日 下午8:40:27 
     */
    final protected function post(string $name = null, $filter = null, $defaultValue = null, bool $noRecursive = false) {
   
        $data = $this->request->getPost();
        return $this->sanitize($data, $name, $filter, $defaultValue, $noRecursive);
    }

    /** 
     * @desc 获取post或者get参数
     * @param string $name 参数名
     * @param string|array $filter 过滤类型,支持string、trim、absint、int、email、float、int!、float!、alphanum、striptags、lower、upper、url、special_chars
     * 当为false时,不使用默认过滤,当为字符串例如'string,trim'时采用参数过滤 ,当为数组例如['string','trim']时采用参数+默认过滤,当为null等其他值时时采用默认过滤
     * @param mixed $defaultValue 默认值
     * @param bool $noRecursive 不递归过滤(即$data不作为数组循环过滤)
     * @return mixed
     * @author zhaoyang 
     * @date 2018年5月9日 下午9:41:49 
     */
    final protected function request
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值