1

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?

ToX 82
  • 1,064
  • 12
  • 35
  • Are you setting or modifying the `viewClassMap` or `inputTypeMap` keys for the request handler? You might be affected by [this change](https://github.com/cakephp/cakephp/pull/7315) - in any event though, please show sufficient code to see/reproduce the problem i.e. the rest of your controller code. – AD7six Sep 24 '15 at 10:09
  • 1
    I would start with comparing the responses (headers and content). – ndm Sep 24 '15 at 10:09
  • No actually I'm not doing anything else with RequestHandler except for loading the component, both in this controller and appController's initialize function. I've updated the question with some more of this controller's code... – ToX 82 Sep 24 '15 at 10:21
  • The headers say: `Content-Type:text/html; charset=UTF-8`, while the content'source code is: ` – ToX 82 Sep 24 '15 at 12:34
  • I have tried to update to CakePHP 3.1.1 but I'm still facing the same problem. The contents looks right, but the header says it's a text/html page, and it's not formatted with his XSL sheet. If I revert to CakePHP 3.0.14 everythings works normally... – ToX 82 Oct 07 '15 at 07:37
  • I've tried again today. With 3.0.x I have "application/xml" as response content-type, while with 3.1 and 3.2 I have "text/html". Everything else is apparently the same. – ToX 82 Jan 07 '16 at 11:28

1 Answers1

0

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