0

我遵循了这个网站上的教程,它解释了如何使用 cakePHP 设置 REST,但是在我完成所有这些之后,当我在浏览器中调用我的控制器时(例如,我有一个具有默认 CRUD 操作的 PostsController),我仍然得到 html作为我的回应(不是xml)。看起来被调用的视图文件是 inapp/views/posts/index.ctp而不是我放置的那个app/views/posts/xml/index.ctp

谁能告诉我这里还有什么我想念的吗?

谢谢

4

3 回答 3

3

您使用的是什么版本的 cakePHP?如果您使用的是 2.x 版本,则解决方案非常简单。

  1. 确保在控制器中包含此行

    public $components = array('RequestHandler');
    
  2. 进入您的 app/Config/routes.php 并在以下行之前添加以下行require CAKE . 'Config' . DS . 'routes.php';

    Router::mapResources('examples');
    Router::parseExtensions('xml');
    

    示例本质上只是您的视图的名称。

  3. 请务必输入带有 xml 扩展名的 url。

    url/controller.xml
    

这应该可以解决问题。包括 RequestHandler 组件,当“与 Router::parseExtensions() RequestHandler 结合使用时,会自动将布局和视图文件切换到与请求类型匹配的那些”

于 2012-10-25T19:24:57.153 回答
0

Did you create an xml layout? You might need to just create a blank layout and in the view set the layout to the xml layout.

于 2012-04-18T23:38:57.020 回答
0

尝试在你的控制器函数中加入这样的东西(或者在 beforeFiler 中更好):

if(@($this->request->isXML() || $this->request->params['ext'] == 'xml')) {
    $this->RequestHandler->respondAs('xml');
}

还要确保在 Config/routes.php 是

Router::parseExtensions('xml');
于 2012-04-18T23:43:16.923 回答