自 cakePHP 3.1 起响应 XML 不起作用 [英] Respond as XML not working since cakePHP 3.1

查看:14
本文介绍了自 cakePHP 3.1 起响应 XML 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用程序中呈现一个 XML+XSL 模板,它曾经用于 cakePHP 3.0.我最近已切换到 3.1,但它已停止工作.问题是我有一个格式化的 XML 视图,而现在我只得到一个纯字符串.

I need to render an XML+XSL template in my application, and it used to work with cakePHP 3.0. I have made the switch to 3.1 recently and it has stopped working. The problem is that I was having a formatted view of my XML, while now I just get a plain string.

迁移指南 说关于 RequestHandlerComponent 中的一些更改,但没有任何帮助(或者可能只是我而我不明白这一点:)).

The migration guide says something about some changes in the RequestHandlerComponent, but nothing helpful (or maybe it's just me and I don't get the point :)).

这是我的控制器(与 Ca​​ke3.0 完全一样):

This is my controller (it is exactly as it was with Cake3.0):

<?php
namespace AppController;

use AppControllerAppController;
use CakeUtilityXml;
use CakeEventEvent;
use CakeRoutingRouter;
use CakeORMTableRegistry;
use CakeFilesystemFolder;
use CakeFilesystemFile;
use CakeNetworkEmailEmail;
use CakeCoreConfigure;
use CakeI18nTime;

/**
 * Invoices Controller
 *
 * @property AppModelTableInvoicesTable $Invoices
 */
class InvoicesController extends AppController
{
    public $components = [
        'Browser',
        'Reorder11'
    ];
    public $helpers = [
        'Multiple'
    ];
    public $paginate = [];

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Paginator');
        $this->loadComponent('RequestHandler');
    }

    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow(['demo']);
    }

    /*
    * ... several other functions ...
    */

    public function viewxml($id = null)
    {
        $this->viewBuilder()->layout('xml');
        $invoice = $this->Invoices->myInvoice($id, $this->Auth->user('id'));

        $this->RequestHandler->respondAs('xml');
        $this->set('invoice', $invoice);
    }
}

xml.ctp 布局,真的很简单

echo $this->fetch('content');

viewxml.ctp 模板只是将 xml 作为字符串回显.

and the viewxml.ctp template just echoes the xml as a string.

如何重新获取格式化后的XML+XSL?

How can I obtain the formatted XML+XSL again?

推荐答案

尝试添加: $this->response->header(['Content-type' => 'application/xml']);

Try add: $this->response->header(['Content-type' => 'application/xml']);

我有同样的错误,但我的输出是 pdf

I had the same error but my output was pdf

使用此代码运行 3.0.14:

working 3.0.14 using this code:

$this->RequestHandler->respondAs("pdf");
$this->layout = 'pdf/default';
$this->view = 'pdf/report1_pdf';

对于 3.1.x(如果你保存文件并稍后打开,这会起作用,如果你尝试直接在浏览器上打开它,它会将纯文件内容打印为 txt/html):

for 3.1.x (this works if u save the file and open later, if you try to open it directly on browser its print the plain file content as a txt/html):

$this->viewBuilder()->layout('pdf/default');
$this->viewBuilder()->template('pdf/report1_pdf');
$this->RequestHandler->respondAs('pdf');
$this->response->header(['Content-type' => 'application/pdf']);

这篇关于自 cakePHP 3.1 起响应 XML 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆