Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
生成XML视图时CakePHP 3出错;第6列第1行出现错误XML声明仅允许在文档开头出现;_Php_Xml_Cakephp_Cakephp 3.0 - Fatal编程技术网

生成XML视图时CakePHP 3出错;第6列第1行出现错误XML声明仅允许在文档开头出现;

生成XML视图时CakePHP 3出错;第6列第1行出现错误XML声明仅允许在文档开头出现;,php,xml,cakephp,cakephp-3.0,Php,Xml,Cakephp,Cakephp 3.0,在使用CakePHP 3生成XML输出时,我花了几个小时试图解决以下问题。我试图用JSON和XML公开数据JSON视图在我使用时非常有效:/bookmarker/bookmarks.JSON但是。。。当我尝试使用此URL:/bookmarker/bookmarks.xml时,我得到以下响应: 此页面包含以下错误: 第6列第1行出错:仅在文档开头允许XML声明 下面是第一个错误之前的页面呈现 视图源如下所示: 有人说,这个错误是因为任何PHP文件末尾有一个空行。我很确定我在以下内容中没有任何空行

在使用CakePHP 3生成XML输出时,我花了几个小时试图解决以下问题。我试图用JSON和XML公开数据JSON视图在我使用时非常有效:/bookmarker/bookmarks.JSON但是。。。当我尝试使用此URL:/bookmarker/bookmarks.xml时,我得到以下响应:

此页面包含以下错误:

第6列第1行出错:仅在文档开头允许XML声明 下面是第一个错误之前的页面呈现

视图源如下所示:

有人说,这个错误是因为任何PHP文件末尾有一个空行。我很确定我在以下内容中没有任何空行:AppController.php、routes.php和BookmarksController.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');
});
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']);
    }
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']);
    }
这是我的配置: 1) Myroutes.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');
});
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']);
    }
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']);
    }
2) MyBookmarksController.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');
});
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']);
    }
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']);
    }
3) 在我的AppController.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');
});
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']);
    }
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']);
    }

如果输出中有空格:您是否还检查了XML输出的视图文件/模板(XML默认布局和索引函数的XML视图)?您好@user1843159谢谢您的回复,我只是删除了这些文件末尾的行:*/vendor/cakephp/cakephp/src/View/View.php****/vendor/cakephp/cakephp/src/View/XmlView.php**没有更改。我仍然有错误。哦,对不起,请不要在供应商中编辑核心文件。如果需要使用核心文件的修改版本,可以在/src/Template/Layout/xml/default.ctp和/src/Template/Bookmarks/xml/index.ctp中创建自己的视图模板问题中发布的代码与您描述的错误模式无关。不管是谁告诉你的,请把所给出的信息当作暗示,而不是你的问题的解决方案或实际原因。错误模式显示,在输出XML文档之前,您向浏览器输出了6个看起来像空白的字符。然后需要找到导致第一次输出的代码。如果手边没有代码库(只需在这里提问),几乎不可能向大家打招呼,谢谢大家的评论和帮助。我终于找到了这个问题的根源;如@AD7six所述,它位于配置文件中。如果输出中有空格:您是否也检查了XML输出的视图文件/模板(XML默认布局和索引函数的XML视图)?您好@user1843159谢谢您的回复,我只是删除了这些文件末尾的行:*/vendor/cakephp/cakephp/src/View/View.php****/vendor/cakephp/cakephp/src/View/XmlView.php**没有更改。我仍然有错误。哦,对不起,请不要在供应商中编辑核心文件。如果需要使用核心文件的修改版本,可以在/src/Template/Layout/xml/default.ctp和/src/Template/Bookmarks/xml/index.ctp中创建自己的视图模板问题中发布的代码与您描述的错误模式无关。不管是谁告诉你的,请把所给出的信息当作暗示,而不是你的问题的解决方案或实际原因。错误模式显示,在输出XML文档之前,您向浏览器输出了6个看起来像空白的字符。然后需要找到导致第一次输出的代码。如果手边没有代码库(只需在这里提问),几乎不可能向大家打招呼,谢谢大家的评论和帮助。我终于找到了这个问题的根源;它位于一个配置文件中,如@AD7six所述。