猿问

返回一个 xml 文件,避免回显它

我知道不应该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什么?


慕神8447489
浏览 119回答 2
2回答

哔哔one

在文档之前,您可以使用该框架,查看如何将字符串作为文件发送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;}

Smart猫小萌

只需使用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());}
随时随地看视频慕课网APP
我要回答