为了账号安全,请及时绑定邮箱和手机立即绑定

返回一个 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 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信