简体   繁体   English

ob_start function 的使用

[英]usage of ob_start function

I'm following this tutorial https://symfony.com/doc/current/create_framework/front_controller.html I have a simple question about this php code我正在关注本教程https://symfony.com/doc/current/create_framework/front_controller.html我对这个 ZE1BFD762321E409CEE4AC0B6E84193 代码有一个简单的问题

 // example.com/web/front.php
require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$request = Request::createFromGlobals();
$response = new Response();

$map = [
    '/hello' => __DIR__.'/../src/pages/hello.php',
    '/bye'   => __DIR__.'/../src/pages/bye.php',
];

$path = $request->getPathInfo();
if (isset($map[$path])) {
    ob_start();
    include $map[$path];
    $response->setContent(ob_get_clean());
} else {
    $response->setStatusCode(404);
    $response->setContent('Not Found');
}

$response->send();

the code works without buffering the output (without with ob_ calls... ), so my question is why?该代码在没有缓冲 output 的情况下工作(没有使用 ob_ 调用...),所以我的问题是为什么?

output buffering takes what is echo ed between ob_start() and ob_get_clean() (or other output buffering ends) and prevents it from "early leaking" into the response that php serves to the user. output 缓冲获取ob_start()ob_get_clean()之间的echo (或其他 output 缓冲结束)并防止它“提前泄漏”到 ZE1BFD762321E409CEEZAC0 提供给用户的响应中。

If you remove output buffering, the content of the response is just output "conventionally", however the $response object would be empty, $reponse->send() would fail, because you already output other stuff, so headers can't be sent anymore. If you remove output buffering, the content of the response is just output "conventionally", however the $response object would be empty, $reponse->send() would fail, because you already output other stuff, so headers can't be发送了。

So, it works, because you either send the output directly (php's default mode of operation), or you buffer the ouput and send that output "indirectly" via the response object (or by just outputting the return value of ob_get_clean ). So, it works, because you either send the output directly (php's default mode of operation), or you buffer the ouput and send that output "indirectly" via the response object (or by just outputting the return value of ob_get_clean ).

the main difference in my opinion is that the framework stuff, that might or might not add some utility to a response, can't do anything anymore, because the framework doesn't know what output you've send.在我看来,主要区别在于框架的东西,可能会或可能不会向响应添加一些实用程序,不能再做任何事情,因为框架不知道你发送了什么 output。 the full symfony framework for example displays a profiler bar at the bottom, which it surely does by injecting it in the response.例如,完整的 symfony 框架在底部显示了一个分析器栏,它肯定会通过在响应中注入它来实现。 By circumventing the $response object, you also deny the injection.通过绕过$response object,您也可以拒绝注入。 however, in your particular case, it might not matter at all.但是,在您的特定情况下,它可能根本不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM