【问题标题】:CakePHP - Just Layout?CakePHP - 只是布局?
【发布时间】:2023-03-29 16:19:01
【问题描述】:

我想在控制器操作中将$this->layout 设置为json

json布局中,会有一行$this->Javascript>object();会解析控制器给它的数据,并输出jSON。

但是,为每个 JSON 请求创建一个新的视图文件,例如。 recipe_view, ingredient_view 不是必须的,我只需要一个布局。

有没有办法完全绕过视图文件,只使用布局,而不会出现臭名昭著的 Missing View! 错误?

【问题讨论】:

    标签: php json cakephp layout view


    【解决方案1】:

    @pleasedontbelong 的解决方案有效。您还可以为 ajax 创建布局和视图。

    你的布局可以是这样的:

    <?php echo $content_for_layout;?>
    

    然后你可以像这样创建一个 ajax 视图:

    <?php echo $this->Js->object($result);?>
    

    然后从你的控制器...

    public function savecontent(){
        $this->autoRender = false;
        $this->set('result', false);
    
        if(!empty($this->data)){
            $data = $this->data;
    
            //Do something with your data
    
            //send results to view
            $this->set('result', $myNewData);
        }
    
        $this->render(null, 'ajax','/ajax/ajax');
    }
    

    【讨论】:

      【解决方案2】:

      嗯,应该是这样的:(未测试)

      function action(){
          $this->autoLayout = $this->autoRender = false;
      
           // your code
      
          $this->render('/layouts/json');
      }
      

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        在 config/routes.php 中:

        Router::parseExtensions('json');
        

        在 app_controller.php 中:

        var $components = array('RequestHandler');
        var $helpers = array('Js');
        
        function render($action = null, $layout = null, $file = null) {
            switch($this->RequestHandler->ext) {
                case 'json':
                    Configure::write('debug', 0);
                    return parent::render(null, 'default', '/json');
                default:
                    return parent::render($action, $layout, $file);
            }
        }
        

        在views/json.ctp:

        <?php echo $this->Js->object(isset($data) ? $data : array()); ?>
        

        在视图/布局/json/default.ctp:

        <?php
        header('Cache-Control: no-store, no-cache, max-age=0, must-revalidate');
        header('Content-Type: application/json');
        echo $content_for_layout;
        ?>
        

        在你要输出json的控制器动作中:

        $this->set('data', array('foo' => 'bar'));
        

        现在每次调用带有 .json 扩展名的操作 (www.example.com/posts/view/12.json) 都会输出一个 json 对象,而无需在每个操作中调用渲染函数。

        【讨论】:

          【解决方案4】:

          CakePHP 2.1 让这一切变得超级简单。 See documentation here.

          1. 在您的 AppController 中,在声明类之后添加这一行:

            public $viewClass = 'Json';

          2. 接下来,将此行添加到控制器操作的末尾,包括您希望以 json 格式显示的任何数据:

            $this->set(compact(array('dataSet1', 'dataSet2')));

            $this->set('_serialize', array('dataSet1', 'dataSet2'));

          就是这样!您不需要为此设置视图。即使你设置了一个,CakePHP 也会忽略它,只显示你在 '_serialize' 数组中指定的变量。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多