有没有办法将 Controller 渲染到与正常 View 不同的 View ?我正在尝试将一些数据从 Controller 传递到非默认 View 。这意味着我的 Controller 被称为:
class StocksRealtimeController extends AppController {
var $uses = 'StockRealtime';
function index(){
$action = '/TestView';
$this->set('stocksRT', $this->StockRealtime->find('all'));
//$this -> viewPath = 'Pages';
$this -> render('/TestView/index');
}
}
... 我的 View 在 views->TestView->index.ctp 中
我的另一个问题是,如何将该值传递给 PHP 而不是 CakePHP 框架之外的 ctp 文件?
我已经尝试了 here 中的所有方法没有运气。
最佳答案
正确的方法:
$this -> render('TestView/index');
正如上面提到的答案,您可以使用 $this -> set
将变量传递给 View 。
但是,如果这不能满足您的需求。我猜您还希望该操作显示另一个布局(非默认布局)。您可以尝试执行 $this -> layout = 'layoutname';
(布局在布局文件夹中,默认为 default.ctp)。
注意: CakePHP 的 Controller 并非设计用于将数据传递给非 View 文件(如 .php)。 CakePHP 的 View 应该以 .ctp
.
关于php - 在 CakePHP 中将 Controller 渲染到不同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11711385/