Cakephp 4 – export xml

AppController + RequestHandlerComponent

class ArticlesController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    }

    public function beforeFilter(EventInterface $event)
    {
        if ($this->RequestHandler->accepts('html')) {
            // Execute code only if client accepts an HTML (text/html) response.
        } elseif ($this->RequestHandler->accepts('xml')) {
            // Execute XML-only code
        }
        if ($this->RequestHandler->accepts(['xml', 'rss', 'atom'])) {
            // Executes if the client accepts any of the above: XML, RSS or Atom.
        }
    }
}

router.php + xml

akcja exportXml – wywołanie .xml – bez serializacji

$this->RequestHandler->respondAs('xml', [

    'attachment' => 'JPK_FA3_'.$from'.-'.$to.'.xml',
    'charset' => 'UTF-8'
]);

Odbiór danych w formacie xml

// Get XML encoded data submitted to a PUT/POST action
$data = $this->request->input('Cake\Utility\Xml::build', ['return' => 'domdocument']);