CakePHP 3创建XML视图 [英] CakePHP 3 creating XML view
本文介绍了CakePHP 3创建XML视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将数组转换为XML响应。目的是在插件控制器中编写一个函数并调用它,该函数将返回XML响应。
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.
我一直在尝试以下CakePHP手册中提到的代码。
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']);
}
}
有没有一种方法可以调试或响应并查看XML响应的样子如何?
Is there a way I can debug or pr the response and see how exactly the XML response will look like?
推荐答案
在routes.php中,插入
in routes.php, insert
Router::extensions('xml');
就在之前
Router::defaultRouteClass('DashedRoute');
然后,只需在操作中使用 .xml即可。
then, just use ".xml" in yout action:
/yourController/index.xml
/yourController/index.xml
这篇关于CakePHP 3创建XML视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文