1

I have spent some hours trying to fix following issue when generating an XML output using CakePHP 3. I am trying to expose the Bookmarks Tutorial data in JSON and XML; JSON view works perfectly when I use: /bookmarker/bookmarks.json but... when I try to use this URL: /bookmarker/bookmarks.xml I have this response:

This page contains the following errors:

error on line 1 at column 6: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error.

The view source looks like this:

enter image description here

Some people says this error is because of a blank line at the end of any PHP file. I am pretty sure I don't have any blank line in: AppController.php, routes.php and BookmarksController.php

This is my configuration:

1) My routes.php

Router::scope('/', function ($routes) {
    // Connect the default routes.
    $routes->fallbacks('InflectedRoute');

    // This allows us to have a RESTful Route for BookmarksController
    $routes->extensions(['json', 'xml']);
    $routes->resources('Bookmarks');
    $routes->resources('Tags');
});

2) My BookmarksController.php

public function index()
    {
        $this->paginate = [
            'contain' => ['Users'],
            'conditions' => [
                'Bookmarks.user_id' => $this->Auth->user('id'),
            ]
        ];
        $this->set('bookmarks', $this->paginate($this->Bookmarks));
        $this->set('_serialize', ['bookmarks']);
    }

3) In my AppController.php

public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Flash');

        // Component to enable REST responses
        $this->loadComponent('RequestHandler');

        $this->loadComponent('Auth', [
            'authorize' => 'Controller', // We will be creating a custom autorization method (see isAuthorized() bellow)
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ],
            'unauthorizedRedirect' => $this->referer()
        ]);

        // Allow the display action so our pages controller
        // continues to work.
        $this->Auth->allow(['display']);
    }
AD7six
  • 63,116
  • 12
  • 91
  • 123
alex-arriaga
  • 117
  • 7
  • If there are whitespaces in the output: Have you also checked the view files / templates for the XML output (XML default layout and XML view for index function)? – Oops D'oh Aug 09 '15 at 03:52
  • Hi @user1843159 thanks for your response I just delete the lines at the end of these files: ** /vendor/cakephp/cakephp/src/View/View.php ** ** /vendor/cakephp/cakephp/src/View/XmlView.php ** Nothing changed. I still have the error. – alex-arriaga Aug 09 '15 at 04:21
  • Oh sorry, please don't edit the core files in vendor. You can create your own view templates in /src/Template/Layout/xml/default.ctp and /src/Template/Bookmarks/xml/index.ctp if you need to use modified versions of the core files – Oops D'oh Aug 09 '15 at 04:47
  • Please also check out [JSON and XML views](http://book.cakephp.org/3.0/en/views/json-and-xml-views.html) and [Class XMLview](http://api.cakephp.org/3.0/class-Cake.View.XmlView.html) - But I'm not sure if the whitespaces are really the problem, sorry. – Oops D'oh Aug 09 '15 at 04:52
  • One way to find where whitespace is coming from is to put ` – AD7six Aug 09 '15 at 12:20
  • Also not that [there is no closing php tag, much less whitespace](https://github.com/cakephp/cakephp/blob/master/src/View/View.php#L1166) at the end of core files - if you're removing "the lines at the end of these files" - what are you removing? I echo @user1843159 in saying that you should modify core files - they __will not__ be the direct source of your problem. – AD7six Aug 09 '15 at 13:04
  • 3
    The code you've posted in the question is unrelated to the error-pattern you describe. Whoever has told you what, please consider the information given as a hint, not as the solution or the actual cause of your problem. The error-pattern shows that you output 6 characters that look like whitespace to the browser before you output the XML document. You then need to find the code that is causing this first output. Without the code-base at hand (by just asking here), this near impossible to say [How can I find out where my output started?](http://stackoverflow.com/q/6685646/367456) – hakre Aug 09 '15 at 14:40
  • 1
    Hello everyone thank you so much for your comments and help. I finally found the origin of this issue; it was in a configuration file as @AD7six mentioned. – alex-arriaga Aug 12 '15 at 03:12
  • Please self answer the question and accept it. – AD7six Aug 17 '15 at 07:38

0 Answers0