CakePHP: How to use a view element inside of a controller

后端 未结 6 1143
情深已故
情深已故 2020-12-17 20:52

I\'m trying to figure out how to use one of my view elements inside of a controller...

I know, I know: \"Don\'t do that!\" (99% of the time this is

6条回答
  •  伪装坚强ぢ
    2020-12-17 21:27

    The way I did any ajax handling in Cake was to have my own AjaxController. Any interaction of ajax-kind goes there, which in-turn uses their own views (and view partials / elements). That way you can keep your code DRY and isolate and propagate all ajax use-cases there.

    Example excerpt:

    autoRender = false;
            $this->layout = false;
            if (!$this->request->is('ajax')) {
                $this->redirect('/');
            }
        }
        public function preview() {
            if ($this->request->is('ajax')) {
                $this->set('data', $this->data);
                $this->render('/Elements/ajaxpreview');
            }
        } 
    ?>
    

    Here's the source: https://github.com/Sobient/dosspirit/blob/master/app/Controller/AjaxController.php

提交回复
热议问题