1

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

迁移指南说了一些关于RequestHandlerComponent.

这是我的控制器(与 Ca​​ke3.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?

4

1 回答 1

0

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

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

使用此代码工作 3.0.14:

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

对于 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']);
于 2015-11-06T15:10:59.153 回答