【问题标题】:Render view to string, then output json in cakephp将视图渲染为字符串,然后在 cakephp 中输出 json
【发布时间】:2014-05-05 08:06:40
【问题描述】:

我目前在使用 AJAX 和 JSON 时遇到了 CakePHP 2.4 的一些问题。

我想用视图呈现数据,但将生成的 html 作为字符串保存在变量中。 之后,我想设置一个数组,其中包含此 html 字符串以及其他数据以作为 JSON 对象返回。不幸的是,我还没有找到正确的方法。

到目前为止,我的控制器代码使用的是 CakePHP json 魔法:

//Controller (just parts) 

$data = $this->paginate();

if($this->request->is('ajax')) {

        $jsonResponse = array(

            'jobs' => $data,

            'foci' => $foci,

            'jobTypes' => $jobTypes,

            'count_number'=> $count_number

        );

        $this->set('jsonResponse', $jsonResponse);

        $this->set('_serialize', 'jsonResponse');

    } else {

        // render regular view
        $this->set(compact('data', 'foci', 'jobTypes', 'count_number'));

    }

这会在 javascript 控制台中输出完美的 json,此外 $data 中的数据是纯数据。

是否有可能将 $data 传递给视图,渲染它,将输出保存到字符串变量 $html,然后将 $html 传递给 jsonResponse 中的作业而不是 $data?

【问题讨论】:

  • 您的意思是您希望您的 json 响应成为 html 的序列化结果吗?如果您要这样做,为什么不直接返回 html。您希望看到的输出的(真实)示例是什么?
  • 感谢您的回复!我想在 Cake 中使用无限页面滚动脚本,我通过 Javascript 附加数据,但我不想用 javascript 格式化数据,而是从服务器获取数据作为 html。此外,我需要一些原始数字来更新页面上的其他元素。

标签: ajax json cakephp cakephp-2.4


【解决方案1】:

是的!您可以将视图渲染为变量。您只需要创建一个视图对象。 在你的控制器里面试试这个:

$view = new View($this,false);
$view->viewPath='Elements';  // Directory inside view directory to search for .ctp files
$view->layout=false; // if you want to disable layout
$view->set ('variable_name','variable_value'); // set your variables for view here
$html=$view->render('view_name'); 

// then use this $html for json response

【讨论】:

    【解决方案2】:

    对于那些使用 CakePhp3

    的人
    $view = new View($this->request,$this->response,null);
    $view->viewPath='MyPath';  // Directory inside view directory to search for .ctp files
    $view->layout='ajax'; // layout to use or false to disable
    $html=$view->render('view_name');
    

    不要忘记在你的命名空间中添加它

    use Cake\View\View;
    

    【讨论】:

    • 感谢 Cake 3.x 兼容代码,在官方文档中找不到。
    【解决方案3】:

    Controller::render() 函数实际上是通过调用CakeResponse::body() 然后返回当前的CakeResponse 对象来设置响应的主体。这意味着您可以在控制器操作中调用render() 方法,捕获其返回值,然后再次调用CakeResponse::body(),从而将响应正文替换为所需的输出。

    示例代码:

    $data = $this->paginate();
    
    // Pass the data that needs to be used in the view
    $this->set(compact('data', 'foci', 'jobTypes', 'count_number'));
    
    if($this->request->is('ajax')) {
    
        // Disable the layout and change the view 
        // so that only the desired html is rendered
        $this->layout = false;
        $this->view = 'VIEW_PASSED_AS_JSON_STRING';
    
        // Call the render() method returns the current CakeResponse object
        $response = $this->render();
    
        // Add any other data that needs to be returned in the response
        // along with the generated html
        $jsonResponse = array(
            'html'       => $response->body(),
            'other_data' => array('foo' => 'bar'),
            'bar'        => 'foo'
        );
    
        // Replace the response body with the json encoded data
        $response->body(json_encode($jsonResponse));
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 2013-08-28
      • 2015-11-01
      相关资源
      最近更新 更多