CakePHP3将视图渲染为变量

Joh*_*hna 4 cakephp cakephp-3.0

我想将视图呈现给变量而不直接将其发送到浏览器.我曾经用cakephp2做过.*.但是,我无法弄清楚如何在CakePHP3中做到这一点.你能告诉我怎么做吗?

Rea*_*lar 9

ViewBuilder在CakePHP 3.1中引入并处理视图的呈现.当我想渲染变量时,我总是看看发送电子邮件的工作原理.

从控制器:

 function index() {
     // you can have view variables.
     $data = 'A view variable';

     // create a builder (hint: new ViewBuilder() constructor works too)
     $builder = $this->viewBuilder();

     // configure as needed
     $builder->layout('default');
     $builder->template('index');
     $builder->helpers(['Html']);

     // create a view instance
     $view = $builder->build(compact('data'));

     // render to a variable
     $output = $view->render();
 }
Run Code Online (Sandbox Code Playgroud)