作为XML响应,因为cakePHP 3.1 [英] Respond as XML not working since cakePHP 3.1

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

问题描述

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



迁移指南说明了 RequestHandlerComponent 中的一些更改, code>,但没有什么帮助(或者也许只是我,我没有得到的点:))。



这是我的控制器因为它与Cake3.0):

 <?php 
namespace App\Controller;

使用App\Controller\AppController;
使用Cake\Utility\Xml;
使用Cake\Event\Event;
使用Cake\Routing\Router;
使用Cake\ORM\TableRegistry;
使用Cake\Filesystem\Folder;
使用Cake \Filesystem\File;
使用Cake\Network\Email\Email;
使用Cake\Core\Configure;
使用Cake\I18n\Time;

/ **
*发票控制器
*
* @property App\Model\Table\InvoicesTable $发票
* /
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']);
}

/ *
* ...几个其他函数...
* /

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 layout,这很简单

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

viewxml.ctp 如何获取格式化的XML + XSL?

解决方案

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



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



工作3.0.14使用这个代码:

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

for 3.1.x(如果您保存文件并稍后打开,它直接在浏览器上打印纯文件内容作为txt / html):

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


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.

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 :)).

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

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Utility\Xml;
use Cake\Event\Event;
use Cake\Routing\Router;
use Cake\ORM\TableRegistry;
use Cake\Filesystem\Folder;
use Cake\Filesystem\File;
use Cake\Network\Email\Email;
use Cake\Core\Configure;
use Cake\I18n\Time;

/**
 * Invoices Controller
 *
 * @property App\Model\Table\InvoicesTable $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);
    }
}

The xml.ctp layout, which is really simple

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

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

How can I obtain the formatted XML+XSL again?

解决方案

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

I had the same error but my output was pdf

working 3.0.14 using this code:

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

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']);

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

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