我知道不应该echo在控制器中使用,但我不明白我应该使用什么来返回 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值。
但在文件末尾有关于标题的警告:
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]
据我所知,这是由于使用了echoin 控制器。在发送标头之前可能会发生输出,然后是警告。
替换功能的正确方法是echo什么?
哔哔one
Smart猫小萌