🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 输出类 输出类是个核心类,它的功能只有一个:发送 Web 页面内容到请求的浏览器。 如果你开启缓存,它也负责 [缓存](http://codeigniter.org.cn/user_guide/general/caching.html) 你的 Web 页面。 注解 这个类由系统自动加载,你无需手工加载。 在一般情况下,你可能根本就不会注意到输出类,因为它无需你的干涉, 对你来说完全是透明的。例如,当你使用 [加载器](http://codeigniter.org.cn/user_guide/libraries/loader.html) 加载一个视图文件时,它会自动传入到输出类,并在系统执行的最后由 CodeIgniter 自动调用。尽管如此,在你需要时,你还是可以对输出进行手工处理。 * [类参考](http://codeigniter.org.cn/user_guide/libraries/output.html#id2) ## [类参考](http://codeigniter.org.cn/user_guide/libraries/output.html#id4) classCI_Output $parse_exec_vars = TRUE; 启用或禁用解析伪变量({elapsed_time} 和 {memory_usage})。 CodeIgniter 默认会在输出类中解析这些变量。要禁用它,可以在你的控制器中设置 这个属性为 FALSE 。 ~~~ $this->output->parse_exec_vars = FALSE; ~~~ set_output($output) 参数: * **$output** (string) -- String to set the output to 返回: CI_Output instance (method chaining) 返回类型: CI_Output 允许你手工设置最终的输出字符串。使用示例: ~~~ $this->output->set_output($data); ~~~ 重要 如果你手工设置输出,这必须放在方法的最后一步。例如, 如果你正在某个控制器的方法中构造页面,将 set_output 这句代码放在方法的最后。 set_content_type($mime_type[, $charset = NULL]) 参数: * **$mime_type** (string) -- MIME Type idenitifer string * **$charset** (string) -- Character set 返回: CI_Output instance (method chaining) 返回类型: CI_Output 允许你设置你的页面的 MIME 类型,可以很方便的提供 JSON 数据、JPEG、XML 等等格式。 ~~~ $this->output ->set_content_type('application/json') ->set_output(json_encode(array('foo' => 'bar'))); $this->output ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php ->set_output(file_get_contents('files/something.jpg')); ~~~ 重要 确保你传入到这个方法的 MIME 类型在 application/config/mimes.php 文件中能找到,要不然方法不起任何作用。 你也可以通过第二个参数设置文档的字符集。 > $this->output->set_content_type('css', 'utf-8'); get_content_type() 返回: Content-Type string 返回类型: string 获取当前正在使用的 HTTP 头 Content-Type ,不包含字符集部分。 ~~~ $mime = $this->output->get_content_type(); ~~~ 注解 如果 Content-Type 没有设置,默认返回 'text/html' 。 get_header($header) 参数: * **$header** (string) -- HTTP header name 返回: HTTP response header or NULL if not found 返回类型: mixed 返回请求的 HTTP 头,如果 HTTP 头还没设置,返回 NULL 。 例如: ~~~ $this->output->set_content_type('text/plain', 'UTF-8'); echo $this->output->get_header('content-type'); // Outputs: text/plain; charset=utf-8 ~~~ 注解 HTTP 头名称是不区分大小写的。 注解 返回结果中也包括通过 PHP 原生的 header() 函数发送的原始 HTTP 头。 get_output() 返回: Output string 返回类型: string 允许你手工获取存储在输出类中的待发送的内容。使用示例: ~~~ $string = $this->output->get_output(); ~~~ 注意,只有通过 CodeIgniter 输出类的某个方法设置过的数据,例如 $this->load->view() 方法,才可以使用该方法获取到。 append_output($output) 参数: * **$output** (string) -- Additional output data to append 返回: CI_Output instance (method chaining) 返回类型: CI_Output 向输出字符串附加数据。 ~~~ $this->output->append_output($data); ~~~ set_header($header[, $replace = TRUE]) 参数: * **$header** (string) -- HTTP response header * **$replace** (bool) -- Whether to replace the old header value, if it is already set 返回: CI_Output instance (method chaining) 返回类型: CI_Output 允许你手工设置服务器的 HTTP 头,输出类将在最终显示页面时发送它。例如: ~~~ $this->output->set_header('HTTP/1.0 200 OK'); $this->output->set_header('HTTP/1.1 200 OK'); $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT'); $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate'); $this->output->set_header('Cache-Control: post-check=0, pre-check=0'); $this->output->set_header('Pragma: no-cache'); ~~~ set_status_header([$code = 200[, $text = '']]) 参数: * **$code** (int) -- HTTP status code * **$text** (string) -- Optional message 返回: CI_Output instance (method chaining) 返回类型: CI_Output 允许你手工设置服务器的 HTTP 状态码。例如: ~~~ $this->output->set_status_header('401'); // Sets the header as: Unauthorized ~~~ [阅读这里](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) 得到一份完整的 HTTP 状态码列表。 注解 这个方法是 [通用方法](http://codeigniter.org.cn/user_guide/general/common_functions.html) 中的 set_status_header() 的别名。 enable_profiler([$val = TRUE]) 参数: * **$val** (bool) -- Whether to enable or disable the Profiler 返回: CI_Output instance (method chaining) 返回类型: CI_Output 允许你启用或禁用 [程序分析器](http://codeigniter.org.cn/user_guide/general/profiling.html) ,它可以在你的页面底部显示 基准测试的结果或其他一些数据帮助你调试和优化程序。 要启用分析器,将下面这行代码放到你的 [控制器](http://codeigniter.org.cn/user_guide/general/controllers.html) 方法的任何位置: ~~~ $this->output->enable_profiler(TRUE); ~~~ 当启用它时,将生成一份报告并插入到你的页面的最底部。 要禁用分析器,你可以这样: ~~~ $this->output->enable_profiler(FALSE); ~~~ set_profiler_sections($sections) 参数: * **$sections** (array) -- Profiler sections 返回: CI_Output instance (method chaining) 返回类型: CI_Output 当程序分析器启用时,该方法允许你启用或禁用程序分析器的特定字段。 请参考 [程序分析器](http://codeigniter.org.cn/user_guide/general/profiling.html) 文档获取详细信息。 cache($time) 参数: * **$time** (int) -- Cache expiration time in seconds 返回: CI_Output instance (method chaining) 返回类型: CI_Output 将当前页面缓存一段时间。 更多信息,请阅读 [文档缓存](http://codeigniter.org.cn/user_guide/general/caching.html) 。 _display([$output = '']) 参数: * **$output** (string) -- Output data override 返回: void 返回类型: void 发送最终输出结果以及服务器的 HTTP 头到浏览器,同时它也会停止基准测试的计时器。 注解 这个方法会在脚本执行的最后自动被调用,你无需手工调用它。 除非你在你的代码中使用了 exit() 或 die() 结束了脚本执行。 例如: ~~~ $response = array('status' => 'OK'); $this->output ->set_status_header(200) ->set_content_type('application/json', 'utf-8') ->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) ->_display(); exit; ~~~ 注解 手工调用该方法而不结束脚本的执行,会导致重复输出结果。