Tencent / Biny

Biny is a tiny, high-performance PHP framework for web applications
BSD 3-Clause "New" or "Revised" License
1.69k stars 259 forks source link

模板有点小问题 #74

Closed ijackwu closed 6 years ago

ijackwu commented 6 years ago

配置 paramsType 使用 one 第二个参数 params 就丢失了,模板无法使用上了 不太理解为什么要使用两个 params,objects

 //响应配置
    'response' => array(
        'jsonContentType' => 'application/json',
        //兼容老版本 新版本都用one就可以了
        'paramsType' => 'one',  // one or keys
        // 以下配置在paramsType == one 时有效
        'paramsKey' => 'PRM',
        'objectEncode' => true, //object对象是否转义
    ),

TXResponse.php


    public function getContent()
    {
        if ($this->config['paramsType'] == 'keys'){
            //老版本兼容过滤XSS
            foreach ($this->params as $key => &$param) {
                if (!in_array($key, $this->objects)){
                    if (is_string($param)){
                        $param = TXString::encode($param);
                    } else if (is_array($param)){
                        $param = new TXArray($param);
                    }
                }
            }
            unset($param);
            extract($this->params);

        } else {
            if (!isset($this->config['objectEncode']) || $this->config['objectEncode']){
                //防XSS注入
                foreach ($this->objects as &$object) {
                    if (is_string($object)){
                        $object = $this->encode($object);
                    } elseif (is_array($object)) {
                        $object = new TXArray($object);
                    }
                }
                unset($object);
            }
            $key = $this->config['paramsKey'];
            $this->objects[$key] = new TXArray($this->params);
            extract($this->objects);
        }

        ob_start();
        //include template
        $lang = TXLanguage::getLanguage();
        $file = sprintf('%s/template/%s%s.tpl.php', TXApp::$app_root, $this->view, $lang ?'.'.$lang : "");
        if (!is_readable($file)){
            $file = sprintf('%s/template/%s.tpl.php', TXApp::$app_root, $this->view);
        }
        if (!is_readable($file)){
            throw new TXException(2005, $this->view);
        }
        include $file;
        TXLogger::showLogs();

        $content = ob_get_clean();
        return $content;
    }
billge1205 commented 6 years ago

并不是丢失了 而是需要使用 <?=$PRM['xxx']?>的方式来使用 PRM 字段名在config中 paramsKey定义 区别在于 可以灵活的使用 $PRM['xxx']$PRM->xxx 来选择性过滤xss 或者原始输出 第三个参数object 主要是用来给object对象使用的 数组/字符串对象建议都放在第二个参数中以达到自动过滤xss的作用

ijackwu commented 6 years ago

$key = $this->config['paramsKey']; $this->objects[$key] = new TXArray($this->params); extract($this->objects);

确实是,3Q