非常教程

Codeigniter 3参考手册

输出 | Output

输出类 | Output Class

Output类是具有一个主要功能的核心类:将最终化的网页发送到请求浏览器。如果您使用该功能,它也负责缓存您的网页。

此类由系统自动初始化,因此不需要手动进行。

在正常情况下,您甚至不会注意到Output类,因为它在透明的情况下运行而无需您的干预。例如,当您使用Loader类加载视图文件时,它会自动传递给Output类,该类将在系统执行结束时由CodeIgniter自动调用。但是,如果需要,可以手动干预输出。

类引用

class CI_Output$parse_exec_vars = TRUE;

启用/禁用解析{elapsed_time}和{memory_usage}伪变量。

默认情况下,CodeIgniter将在输出中解析这些标记。若要禁用此属性,请在控制器中将此属性设置为false。

$this->output->parse_exec_vars = FALSE;

set_output($output)

参数:

$ output(string) - 将输出设置为的字符串

返回:

CI_Output实例(方法链接)

返回类型:

CI_Output

  • $ outputstring) - 将输出设置为的字符串返回:CI_Output实例(方法链接)返回类型:CI_Output允许您手动设置最终输出字符串。用法示例:$ this-> output-> set_output($ data); 重要如果您手动设置输出,它必须是您调用它的函数中完成的最后一件事。例如,如果您在其中一个控制器方法中构建页面,则不要将输出设置为结束。set_content_type($mime_type[, $charset = NULL])参数:$ mime_type(string) - MIME类型idenitifer字符串$ charset(字符串) - 字符集返回:CI_Output实例(方法链接)返回类型:CI_Output
  • $ mime_typestring) - MIME类型idenitifer字符串
  • $ charset字符串) - 字符集
Returns:  CI\_Output instance (method chaining)
Return type:  CI\_Output
Permits you to set the mime-type of your page so you can serve JSON data, JPEG’s, XML, etc easily.

$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()

返回:

内容类型字符串

返回类型:

get_header($header)

参数:

$ header(string) - HTTP标头名称

返回:

HTTP响应头或NULL,如果未找到

返回类型:

mixed

  • $ headerstring) - HTTP头名称返回:HTTP响应头或NULL,如果未找到返回类型:mixed返回请求的HTTP头值,或者如果未设置请求的头,则返回NULL。例如:$ this-> output-> set_content_type('text / plain','UTF-8'); echo $ this-> output-> get_header('content-type'); //输出:text / plain; charset = utf-8注意标题名称以不区分大小写的方式进行比较。注意header()还会检测通过PHP本地函数发送的原始头文件。get_output()返回:输出string返回类型:字符串append_output($output)参数:$ output(string) - appendReturns的附加输出数据:CI_Output实例(方法链接)返回类型:CI_Output
  • $ outputstring) - 附加的输出数据
Returns:  CI\_Output instance (method chaining)
Return type:  CI\_Output
将数据追加到输出字符串中。

$this - >输出 - > append_output($数据);

set_header($header[, $replace = TRUE])

参数:

$ header(string) - HTTP响应头文件$ replace(bool) - 是否替换旧的头文件值(如果已设置)

返回:

CI_Output实例(方法链接)

返回类型:

CI_Output

  • $ headerstring) - HTTP响应头
  • $ replacebool) - 是否替换旧的标题值,如果它已经设置
Returns:  CI\_Output instance (method chaining)
Return type:  CI\_Output
Permits you to manually set server headers, which the output class will send for you when outputting the final rendered display. Example:

$ 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 MYH: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状态码$ text(字符串) - 可选消息

返回:

CI_Output实例(方法链接)

返回类型:

CI_Output

  • $ codeint) - HTTP状态码
  • $ textstring) - 可选消息
Returns:  CI\_Output instance (method chaining)
Return type:  CI\_Output
允许您手动设置服务器状态标题。例:

$this- >输出 - > set_status_header(401); //将头部设置为:未授权

看这里有关标题的完整列表。

此方法是公共函数set_status_header()...

enable_profiler([$val = TRUE])

参数:

$ val(bool) - 是否启用或禁用Profiler

返回:

CI_Output实例(方法链接)

返回类型:

CI_Output

  • $ valbool) - 是否启用或禁用Profiler返回:CI_Output实例(方法链)返回类型:CI_Output允许您启用/禁用Profiler,它将在页面底部显示基准测试和其他数据以进行调试和优化目的。要启用Profiler,请在Controller方法的任何位置放置以下行:$ this-> output-> enable_profiler(TRUE); 启用后,报告将生成并插入到页面的底部。要禁用分析器,您可以使用:$ this-> output-> enable_profiler(FALSE); set_profiler_sections($sections)参数:$ sections(array) - Profiler节返回:CI_Output实例(方法链)返回类型:CI_Output
  • $ sections数组) - Profiler部分
Returns:  CI\_Output instance (method chaining)
Return type:  CI\_Output
允许您在启用时启用/禁用Profiler的特定部分。有关更多信息,请参阅[Profiler](../ general / profiling)文档。

cache($time)

参数:

$ time(int) - 以分钟为单位的缓存过期时间

返回:

CI_Output实例(方法链接)

返回类型:

CI_Output

  • $ timeint) - 以分钟为单位的缓存过期时间返回:CI_Output实例(方法链接)返回类型:CI_Output将当前页面缓存指定的分钟数。有关更多信息,请参阅缓存文档。_display([$output = ''])参数:$ output(string) - 输出数据overrideReturns:void返回类型:void
  • $ outputstring) - 输出数据覆盖
Returns:  void
Return type:  void
Sends finalized output data to the browser along with any server headers. It also stops benchmark timers.

此方法在脚本执行结束时自动调用,除非您使用以下命令中止脚本执行,否则不需要手动调用它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;

手动调用此方法而不中止脚本执行将导致重复输出。

输出 | Output相关

Codeigniter 3

CodeIgniter 是一个PHP MVC框架,特点是超轻量级、有数据加密、有灵活URI路由等。对于 PHP 程序员来说,它小巧但功能强大。

主页 https://codeigniter.com/
源码 https://github.com/bcit-ci/CodeIgniter
版本 3
发布版本 3.1.5