亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

返回一個 xml 文件,避免回顯它

返回一個 xml 文件,避免回顯它

PHP
慕神8447489 2022-07-16 16:43:29
我知道不應該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什么?
查看完整描述

2 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

在文檔之前,您可以使用該框架,查看如何將字符串作為文件發送


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;

}


查看完整回答
反對 回復 2022-07-16
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

只需使用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());

}


查看完整回答
反對 回復 2022-07-16
  • 2 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號