【问题标题】:How to Test cakephp parseExtension?如何测试 cakephp parseExtension?
【发布时间】:2013-01-25 02:45:23
【问题描述】:

我在我的 cakephp 2.3.0 中为 json 设置了 parseExtension。不显示错误。有效吗?

我该如何测试?

在 RoR 中很容易测试通过

https://mydomain.de/view/4.json

如何在 cakephp 上运行?

我的 View-Action 是这样的。

 public function view($id = null) {
            if (!$this->Atla->exists($id)) {
                    throw new NotFoundException(__('Invalid atla'));
            }
            $options = array('conditions' => array('Atla.' . $this->Atla->primaryKey => $id));
            $this->set('atla', $this->Atla->find('first', $options));

            $this->Atla->id = $id;
            $result = $this->Atla->read();
            $this->response->type('json');
            $this->response->body(json_encode($result));
            return $this->response;   
            $this->set(compact('atlas'));

    }

为什么我总是收到 json 请求?

【问题讨论】:

  • 如果我在浏览器中输入这个,我会收到错误{"code":500,"url":"\/Atlas\/view\/2.json","name":"View file "\/usr\/local\/www\/cakephp\/app\/View\/Atlas\/json\/view.ctp" is missing."}
  • 那么它似乎工作了!您所要做的就是创建视图 app/View/Atlas/json/view.ctp,这是用于 .json 请求的视图。 没有 .json 的请求将使用app/View/Atlas/view.ctp :) 这里有更多关于创建/使用 JSON 和 XML 视图的信息:book.cakephp.org/2.0/en/views/json-and-xml-views.html
  • 这是一个普通的(c&p)视图文件还是应该是空的?
  • 我在下面的答案中做了解释,因为 cmets 没有提供太多选项来格式化代码并保持可读性。希望对你有帮助

标签: php cakephp controller components cakephp-2.3


【解决方案1】:

如果您使用_serialize 键 cakephp 可以自动为您创建 json 和 xml 视图。我通常使用以下内容来创建 json 或 xml 视图:

public function view() {
  // data I want to display
  $record1 = $this->ModelName->find('first', ...);
  $this->set('record1', $record1);

  $record2 = $this->ModelName->find('first', ...);
  $this->set('record2', $record2);


  // option 1: serialize the hard way 
  $this->set('_serialize', array('record1', 'record2'));

  // option 2: serialize the easy way
  $this->set('_serialize', array_keys($this->viewVars));
}

PS:return 语句后面的代码永远不会被执行。

【讨论】:

    【解决方案2】:

    您将创建视图

    app/View/Atlas/json/view.ctp

    这是用于 .json 请求的视图。 没有 .json 的请求将使用常规视图文件:

    app/View/Atlas/view.ctp
    

    这里有更多关于创建/使用 JSON 和 XML 视图的信息: http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#using-a-data-view-with-view-files

    从该页面 view.ctp 可能包含类似的内容;

    // View code - app/View/Posts/json/index.ctp
    foreach ($posts as &$post) {
        unset($post['Post']['generated_html']);
    }
    echo json_encode(compact('posts', 'comments'));
    

    但是,这实际上取决于您要实现的目标。如果您只对 JSON 响应使用“Atlas/view”操作,而根本不使用 HTML,则有时您可以在 Controller 中生成响应体。与 MVC 约定不太“符合”,但它使您免于创建仅执行 echo json_encode($data); 的视图;)

    public function view($id)
    {
        $this->MyModel->id = $id;
        $result = $this->MyModel->read();
    
        $this->response->type('json');
        $this->response->body(json_encode($result));
    
        //Return reponse object to prevent controller from trying to render a view
        return $this->response;
    }
    

    如果您确实想要同时使用“HTML”和“JSON”,具体取决于请求(带/不带 .json 扩展名),您应该有 两个 视图文件; 1 表示 JSON,1 表示 HTML;

    // This view will be used for your JSON requests
    app/View/Atlas/json/view.ctp
    
    // This view will be used for non-JSON (html) requests:
    app/View/Atlas/view.ctp
    

    在 json-view 中,使用 json_encode(.....) 输出数据; 在'normal'/html视图中,只输出普通数据

    在您的控制器中,将数据设置为正常

    public function view($id = null) {
            $this->Atla->id = $id;
            if (!$this->Atla->exists()) {
                    throw new NotFoundException(__('Invalid atla'));
            }
            $this->set('atla', $this->Atla->read());
    
    }
    

    【讨论】:

    • 我总是收到 json 请求...没有 html :-( 我用视图控制器编辑我的问题。
    • 也就是说,因为在您的代码中,您总是返回 JSON 响应。如果您想使用 both HTML 或 JSON,则不应在控制器中返回响应,而应创建两个单独的视图文件;我会更新我的分析器
    • json 视图是你的方式。这运行良好谢谢 html-view 已烘焙。但是给我一个带有样式的html
    • <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> CakePHP: the rapid development php framework: Atlas </title> <link href="/favicon.ico" type="image/x-icon" rel="icon" /><link href="/favicon.ico" type="image/x-icon" rel="shortcut icon" /><link rel="stylesheet" type="text/css" href="/css/cake.generic.css" /></head> <body> <div id="container"> <div id="header"> <h1><a href="http://cakephp.org">CakePHP: the rapid development php framework</a></h1> </div> <div id="content">
    • 您看到的样式是使用的“布局”。视图在布局中呈现。默认布局可以在这里找到app/View/Layouts/default.ctp。但是对于此类问题,请查看文档,
    猜你喜欢
    • 2012-04-24
    • 2017-10-14
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多