五十四、逐行阅读Yii2.0.43_Yii框架文件yii\web\Response.php(6)

一、属性

1. $_cookies CookieCollection实例

    //CookieCollection实例
    private $_cookies;

二、方法

1. getCookies方法,返回CookieCollection实例

    // 返回CookieCollection实例
    public function getCookies()
    {
        if ($this->_cookies === null) {
            $this->_cookies = new CookieCollection();
        }

        return $this->_cookies;
    }

2. defaultFormatters方法,默认支持的响应内容格式

    // 默认支持的响应内容格式
    protected function defaultFormatters()
    {
        return [
            self::FORMAT_HTML => [
                'class' => 'yii\web\HtmlResponseFormatter',
            ],
            self::FORMAT_XML => [
                'class' => 'yii\web\XmlResponseFormatter',
            ],
            self::FORMAT_JSON => [
                'class' => 'yii\web\JsonResponseFormatter',
            ],
            self::FORMAT_JSONP => [
                'class' => 'yii\web\JsonResponseFormatter',
                'useJsonp' => true,
            ],
        ];
    }

3. prepare方法,准备发送响应

    //准备发送响应
    protected function prepare()
    {
        if (in_array($this->getStatusCode(), [204, 304])) {
            // A 204/304 response cannot contain a message body according to rfc7231/rfc7232
            $this->content = '';
            $this->stream = null;
            return;
        }

        if ($this->stream !== null) {
            return;
        }

        if (isset($this->formatters[$this->format])) {
            $formatter = $this->formatters[$this->format];
            if (!is_object($formatter)) {
                $this->formatters[$this->format] = $formatter = Yii::createObject($formatter);
            }
            if ($formatter instanceof ResponseFormatterInterface) {
                $formatter->format($this);
            } else {
                throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface.");
            }
        } elseif ($this->format === self::FORMAT_RAW) {
            if ($this->data !== null) {
                $this->content = $this->data;
            }
        } else {
            throw new InvalidConfigException("Unsupported response format: {$this->format}");
        }

        if (is_array($this->content)) {
            throw new InvalidArgumentException('Response content must not be an array.');
        } elseif (is_object($this->content)) {
            if (method_exists($this->content, '__toString')) {
                $this->content = $this->content->__toString();
            } else {
                throw new InvalidArgumentException('Response content must be a string or an object implementing __toString().');
            }
        }
    }

4. isSeekable方法,检查流是否可查找

    //检查流是否可查找
    private function isSeekable($handle)
    {
        if (!is_resource($handle)) {
            return true;
        }

        $metaData = stream_get_meta_data($handle);
        return isset($metaData['seekable']) &&
            $metaData['seekable'] === true;
    }

总结:

 阅读了1个属性和4个方法:

  • $_cookies CookieCollection实例
  • getCookies方法,返回CookieCollection实例
  • defaultFormatters方法,默认支持的响应内容格式
  • prepare方法,准备发送响应
  • isSeekable方法,检查流是否可查找
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值