Return 一个 xml 文件避免回显它

Return an xml file avoiding to echo it

我知道不应该在控制器中使用 echo,但我不明白我应该使用 return 和 xml 来下载它。请注意,它不是服务器上的文件,它只是一个字符串:

public function export()
{
    $this->autoRender = false;

    $id = $this->request->getQuery('id');
    $invoice = $this->Invoices->get($id, ['contain' => ['Customers', 'ItemInvoices' => ['ItemProformas' => ['ItemDeliveryNotes' => ['ItemOrders' => ['Orders' => ['Customers']]]]]]]);

    $fpr = new ExportInvoice();
    $fpr->SetInvoice($invoice);

    header('Content-type: text/xml');
    header('Content-Disposition: attachment; filename="' . $fpr->getFilename() . '"');

    $xml = $fpr->asXML();
    echo $xml;
}

它实际上按预期工作:浏览器下载具有给定文件名的文件,其内容是 $xml 值。

但是在文件末尾有关于 headers:

的警告
Warning (512): Unable to emit headers. Headers sent in file=/home/mark/myproject/src/Controller/InvoicesController.php line=130 [CORE/src/Http/ResponseEmitter.php, line 51]
Warning (2): Cannot modify header information - headers already sent by (output started at /home/mark/myproject/src/Controller/InvoicesController.php:130) [CORE/src/Http/ResponseEmitter.php, line 152]
Warning (2): Cannot modify header information - headers already sent by (output started at /home/mark/myproject/src/Controller/InvoicesController.php:130) [CORE/src/Http/ResponseEmitter.php, line 181]
Warning (2): Cannot modify header information - headers already sent by (output started at /home/mark/myproject/src/Controller/InvoicesController.php:130) [CORE/src/Http/ResponseEmitter.php, line 181]

据我所知,这是由于在控制器中使用了 echo。在发送 header 之前可能会发生输出,然后是警告。

替换echo函数的正确方法是什么?

只需使用die()exit()

public function export()
{
    $this->autoRender = false;

    $id = $this->request->getQuery('id');
    $invoice = $this->Invoices->get($id, ['contain' => ['Customers', 'ItemInvoices' => ['ItemProformas' => ['ItemDeliveryNotes' => ['ItemOrders' => ['Orders' => ['Customers']]]]]]]);

    $fpr = new ExportInvoice();
    $fpr->SetInvoice($invoice);

    if (!headers_sent())
    {
        header('Content-type: text/xml');
        header('Content-Disposition: attachment; filename="' . $fpr->getFilename() . '"');
    }
    else
    {
        //Do something else to let them know they can't expect a file
        die();
    }

    die($fpr->asXML());
}

更新

关于评论

使用return, exit or die is already explained in detail in this question php-exit-or-return-which-is-better, what-are-the-differences-in-die-and-exit-in-php

在文档之前,您可以为此使用框架,查看如何 Sending a String as File

public function export()
{
    $this->autoRender = false;

    $id = $this->request->getQuery('id');
    $invoice = $this->Invoices->get($id, ['contain' => ['Customers', 'ItemInvoices' => ['ItemProformas' => ['ItemDeliveryNotes' => ['ItemOrders' => ['Orders' => ['Customers']]]]]]]);

    $fpr = new ExportInvoice();
    $fpr->SetInvoice($invoice);

    // header('Content-type: text/xml');
    // header('Content-Disposition: attachment; filename="' . $fpr->getFilename() . '"');

    $xml = $fpr->asXML();
    $response = $this->response;
    $response = $response->withStringBody($xml);
    // use $response->body($xml); for versions before 3.4.0
    $response = $response->withType('xml');
    $response = $response->withDownload($fpr->getFilename());
    return $response;
}