自cakePHP 3.1起响应XML不起作用

时间:2015-09-24 09:26:22

标签: cakephp cakephp-3.1

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

migration guide说明了RequestHandlerComponent中的某些变化,但没有任何帮助(或者它只是我,我不明白这一点:)。< / p>

这是我的控制器(它与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);
    }
}

xml.ctp布局,非常简单

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

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

如何再次获取格式化的XML + XSL?

1 个答案:

答案 0 :(得分:0)

尝试添加:$ this-&gt; response-&gt; header([&#39; Content-type&#39; =&gt;&#39; application / xml&#39;]);

我有同样的错误,但我的输出是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']);