Tuesday, March 15, 2022

[FIXED] CakePHP 3 creating XML view

Issue

I need to convert an array to an XML response. The objective is to write a function within a plugin controller and call it, which would return an XML response.

I've been trying this below mentioned code from the CakePHP manual.

   namespace App\Controller;

   class ArticlesController extends AppController{

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

       public function index(){

            // Set the view vars that have to be serialized.
            $this->set('articles', $this->paginate());
            // Specify which view vars JsonView should serialize.
            $this->set('_serialize', ['articles']);
       }
    }

Is there a way I can debug or pr the response and see how exactly the XML response will look like?


Solution

in routes.php, insert

Router::extensions('xml');

just before

Router::defaultRouteClass('DashedRoute');

then, just use ".xml" in yout action:

/yourController/index.xml



Answered By - Fabio Colombini

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.