2015-09-24 22 views
1

我需要在我的應用程序中呈現XML + XSL模板,並且它用於使用cakePHP 3.0。我最近切換到了3.1,並且它已停止工作。問題是我有一個格式化的XML視圖,而現在我只是得到一個普通的字符串。由於XML無法正常工作,因爲cakePHP 3.1

migration guide說了一些有關RequestHandlerComponent的一些變化,但沒有什麼幫助(或者只是我,我不明白:))。

這是我的控制器(正是因爲它與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?

+0

你是否設置或修改請求處理程序的'viewClassMap'或'inputTypeMap'鍵?您可能會受[此更改](https://github.com/cakephp/cakephp/pull/7315)的影響 - 無論如何,請顯示足夠的代碼來查看/重現問題,即您的控制器代碼的其餘部分。 – AD7six

+1

我會開始比較迴應(標題和內容)。 – ndm

+0

實際上,除了加載組件,無論是在此控制器還是appController的初始化函數中,實際上我都沒有對RequestHandler做任何其他操作。我更新了這個控制器的代碼更多的問題... –

回答

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(這個工程如果u保存文件並在稍後打開,如果你試圖直接在瀏覽器打開它的純文本內容作爲TXT/HTML):

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

我已經嘗試過它,現在3.1和3.1RC1 ...仍然無法正常工作。 –

相關問題