Yaf继承Yaf_Request_Http获取参数并验证

0 篇文章 0 订阅
Yaf_Request_Http

代表了一个实际的Http请求, 一般的不用自己实例化它, Yaf_Application在run以后会自动根据当前请求实例它,在控制器内可以使用$this->getRequest()来获取请求信息。

使用示例
//如果Yaf配置开启了命名空间,需要改成Yaf\Controller_Abstract

class IndexController extends Yaf_Controller_Abstract
{
    public function indexAction($name = '', $value = '')
    {
        print_r($this->getRequest()->getQuery());
    }
}

扩展Yaf_Request_Http,比如加上过滤,数据处理等。先在library定义一个request的类,再在Bootstrap.php里设置Request

文件示例:library/Request.php
//如果Yaf配置开启了命名空间,需要改成Yaf\Request\Http

class Request extends Yaf_Request_Http
{
    private $_posts;
    private $_params;
    private $_query;

    public function getPost()
    {
        if ($this->_posts) {
            return $this->_posts;
        }
        $this->_posts = $this->filter_params(parent::getPost());
        return $this->_posts;
    }

    public function getParams()
    {
        if ($this->_params) {
            return $this->_params;
        }
        $this->_params = $this->filter_params(parent::getParams());
        return $this->_params;
    }

    public function getQuery()
    {
        if ($this->_query) {
            return $this->_query;
        }
        $this->_query = $this->filter_params(parent::getQuery());
        return $this->_query;
    }

    private function filter_params($params)
    {
        if (!empty($params)) {
            array_walk_recursive($params, function (&$value, $key) {
                $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
            });
        }
        return $params;
    }
}
文件示例:Bootstrap.php
//如果Yaf配置开启了命名空间,需要改成Yaf\Bootstrap_Abstract

class Bootstrap extends Yaf_Bootstrap_Abstract
{
    public function _initRequest(Yaf_Dispatcher $dispatcher)
    {
        $dispatcher->setRequest(new Request());
    }
}

然后在控制器中可以使用$this->getRequest()->getQuery()来获取参数

//如果Yaf配置开启了命名空间,需要改成Yaf\Controller_Abstract

class IndexController extends Yaf_Controller_Abstract
{
    public function indexAction()
    {
        print_r($this->getRequest()->getQuery());
    }
}

来源:https://www.kancloud.cn/liustone/yaf-note/84332

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值